开发者

How to search for record in mysql using php?

Thanks for all your help, I now have add, update, and delete functions on my database manipulati开发者_Python百科on program. But I'm having difficulty on searching for records in mysql database. Please help me in correcting my codes, and if you know a site that I can use as my reference please do tell. Thanks, here is my search code:

         <?php
      $con = mysql_connect("localhost","root","");
      if (!$con)
   {
     die('Could not connect: ' . mysql_error());
   }

      mysql_select_db("koro", $con);

     mysql_query("UPDATE student 
    WHERE IDNUMBER ='$_POST[INAME]'");

    while ($row = mysql_fetch_array($query)) 
    { 
      $variable1=$row["IDNUMBER"]; 
      $variable2=$row["LNAME"]; 
      $variable3=$row["FNAME"]; 
      $variable3=$row["MNAME"]; 
      $variable3=$row["GRADEYR"]; 
      $variable3=$row["ADDRESS"]; 
          } 

       mysql_close($con);

      ?>

And here is the html form that I'm using, and I think this is where the problem is. I don't have any idea on how to put the results in the text box.


Check the documentation for mysql_query: it returns TRUE or FALSE for UPDATE queries. If you want the new values, follow the UPDATE with a select.

More importantly, your code is vulnerable to SQL injection. The best solution is to switch from the outdated mysql driver to PDO.

Unpacking an array into separate variables (as you do) is unnecessary. It's also problematic: notice how you've mistyped the last few names as $variable3.

Also, don't use "or die" (except in very limited circumstances).


<?php
      $con = mysql_connect('host','user','pass',db_name);
      if (!$con)
   {
     echo ('Could not connect: ' . mysql_error());
   }

      mysql_select_db("koro", $con);

     $query=mysql_query("UPDATE student 
    WHERE IDNUMBER ='$_POST[INAME]'");

    while ($row = mysql_fetch_array($query)) 
    { 
      $var1=$row["IDNUMBER"]; 
      $var2=$row["LNAME"]; 
      $var3=$row["FNAME"]; 
      $var4=$row["MNAME"]; 
      $var5=$row["GRADEYR"]; 
      $var5=$row["ADDRESS"]; 
          } 

       mysql_close($con);

      ?>

Don't use die (); in code and define the variable to query to pass an argument or write query in argument. For further information check the documentation on PHP official web site or w3school.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜