Home>

By studying, I am making a customer search system with PHP and HTML.

The customer search system is a system in which when you enter a customer name and customer number and press the search button, the data (customer phone number, customer address) that matches the input contents is retrieved from the database and automatically entered.

If either or both of the customer name and customer number have not been entered, I would like to be able to output the message "Please enter."

Also, if there is no applicable data, I would like to be able to output a back button with the message "There is no applicable data. Please re-enter."

I get an error message, and even if I correct it, I get the same error message.

What kind of program should be created to create the ideal system?
(You can ignore menu.php.)

Warning: Undefined array key "pkey" in C: \ xampp \ htdocs \ customer \ c_search.php on line 2
Fatal error: Uncaught Error: Call to undefined function mysql_connect () in C: \ xampp \ htdocs \ customer \ c_search.php: 4 Stack trace: # 0 {main} thrown in C: \ xampp \ htdocs \ customer \ c_search.php on line 4
Corresponding source code
<? php
$pkey = trim ($_REQUEST ['pkey']);
$db = mysql_connect ('localhost','user','pass');
$rc = mysql_select_db ('example');
$result = mysql_query ("select * from example");
$result = mysql_query ($query);
if (! $result) {
    $message ='Invalid query:'. mysql_error (). "\ n";
    $message. ='Whole query:'. $Query;
    die ($message);
}
while ($row = mysql_fetch_assoc ($result)) {
    echo $row ['C_name'];
    echo $row ['C_num'];
    echo $row ['C_phon'];
    echo $row ['C_sa'];
}
mysql_free_result ($result);
?>
<! DOCTYPE html PUBLIC "-// W3C // DTD XHTML 1.0 Transitional // EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "ja" lang = "ja">
<head>
<meta charset = "UTF-8">
<meta name = "viewport" content = "width = device-width">
<title>Customer search</title>
<link rel = "stylesheet" href = "ser_style.css">
</head>
<body>

  Customer search 
<label for = "C_name">Customer name<span>Required</span></label>
<input type = "text" name = "C_name">
<label for = "C_num">Customer number<span>Required</span></label>
<input type = "number" name = "C_num">
<label for = "C_phon">Customer Phone Number</label>
<input type = "tel" name = "C_phon">
<label for = "C_sa">Customer Address</label>
<input type = "text" name = "C_sa">
<button type = "submit">Search</button>
<form action = "menu.php" method = "GET">
 <button type = "submit">Go to menu</button>
</form>

</body>
</html>
What I tried

・ I tried to get an error message, but it doesn't work.
・ I wrote a PHP statement in my own way

Supplementary information (FW/tool version, etc.)

The software used is xammp, the file name is C_search.php and the CSS file is ser_style.css.

  • Answer # 1

    I'm using a function that starts with mysql_, but are there any documents or books that I refer to?
    The functions starting with mysql_ have various problems, and the successor mysqliIt has been replaced with a group of functions starting with _.
    (For the time being, just add i.)
    Let's change the function that should not be used before, such as not being able to access the database, to a mysqli function. Alternatively, it is recommended to completely change to the method of using PDO.

    PHP: Which API to use --Manual

    For the basic method of using PDO, refer to here.
    It is written that it is important enough for veterans to read it again.

    Summary when connecting to a database with PHP-Qiita

    If you're referring to an old book or document,
    Switch to one that can be confirmed to be compatible with PHP7 or later.
    Even if you learn the old method now, you will only relearn it, and forcibly running the old PHP is not preferable for vulnerability countermeasures.
    As explained in the mysql_ system, it may be a problem that transaction processing is not performed.

    $_REQUEST ['pkey']The way of referencing is not good.
    It should be implemented after clearly designing whether to receive by POST or GET.
    If that consciousness is weak, it will adversely affect the system by illegally receiving parameters from the outside unintentionally.

    And I suddenly refer to it even though I don't know if it exists or not
    $pkey = trim ($_REQUEST ['pkey']);
    That is not good.
    (Warning: Cause of Undefined array key "pkey")
    PHP: filter_input --Manual
    It is easy to make full use of.

  • Answer # 2

    mysql_connect ()

    Is an obsolete function and cannot be used.
    Strictly speaking, it's a function that you can use if you have an older version of PhP installed, but you shouldn't even use it.

    If it is listed in any teaching material, it is too old to be used, so it is strongly recommended to change to a new teaching material.

  • Answer # 3

    mysql_connect ()Is a function that has been deprecated long ago. Not available in recent versions of php.
    Perhaps the textbook I'm referring to is old, so it's a good idea to change it to a textbook that supports at least php 7 or later.

Related articles