1

我要将一行更新到 mysql 数据库中。senarius 是:从表单中获取值并重定向到另一个文件,并使用 update 语句将表单值设置为数据库。问题是 mysql_query 返回值 1 并且不返回任何错误,但是当我通过 phpmyadmin 检查数据库时,我的数据库不受影响。这是代码

<?php
    $host="localhost";
    $username="root";
    $password="";
    $db_name="login_takrim";
    $tbl_name="takrim_users";
    // Connect to server and select databse.
    mysql_connect("c$host","$username","$password") or die("can not connect");
    mysql_select_db($db_name) or die(mysql_error());
    // username and password sent from form 
    $myusername=$_POST["txtusername"];
    $mypassword=$_POST["txtpassword"];
    $myemail=$_POST["txtemail"];

    // To protect MySQL injection
    $myusername=stripslashes($myusername);
    $myemail=stripslashes($myemail);
    $mypassword=stripslashes($mypassword);
    $myemail=mysql_real_escape_string($myemail);
    $myusername=mysql_real_escape_string($myusername);
    $mypassword=mysql_real_escape_string($mypassword);
    echo "$myusername $mypassword $myemail";// test to see i get the form value on the php server.
    $sql="UPDATE $tbl_name SET username = '$myusername' and  password = '$mypassword' and email= '$myemail' where showname='hussein'";

    $result=mysql_query($sql) or die(mysql_error());//does not return error 
    echo $result;
    if($result==false)
    {
        echo "no";
    }
    else
    {
        //header("location:setEmail.php");
        echo "yes";

    }


?>
4

3 回答 3

0

像这样改变你的UPDATE陈述

$sql="UPDATE $tbl_name SET `username` = '$myusername',`password` = '$mypassword',`email`= '$myemail' where `showname`='hussein'";

免责声明:停止使用 mysql_* 函数,因为它们已被弃用。改用 MySQLi 或 PDO。

于 2013-11-06T17:50:22.563 回答
0

你有一个额外的c在这里(之前$host):

mysql_connect("c$host","$username","$password") or die("can not connect");
于 2013-11-06T17:51:16.343 回答
0

查询可能正确执行可能没有匹配的记录只是这样做

    <?php
        $host="localhost";
        $username="root";
        $password="";
        $db_name="login_takrim";
        $tbl_name="takrim_users";
        // Connect to server and select databse.
        mysql_connect("c$host","$username","$password") or die("can not connect");
        mysql_select_db($db_name) or die(mysql_error());
        // username and password sent from form 
        $myusername=$_POST["txtusername"];
        $mypassword=$_POST["txtpassword"];
        $myemail=$_POST["txtemail"];

        // To protect MySQL injection
        $myusername=stripslashes($myusername);
        $myemail=stripslashes($myemail);
        $mypassword=stripslashes($mypassword);
        $myemail=mysql_real_escape_string($myemail);
        $myusername=mysql_real_escape_string($myusername);
        $mypassword=mysql_real_escape_string($mypassword);
        echo "$myusername $mypassword $myemail";// test to see i get the form value on the php server.
        $sql="UPDATE $tbl_name SET username = '$myusername',  password = '$mypassword',email= '$myemail' where showname='hussein'";

        $result=mysql_query($sql) or die(mysql_error());//does not return error 
         if(mysql_num_rows($result) > 0)
         {   
             //header("location:setEmail.php");
            echo "yes";

         }
         else
         {
              echo "no";
          }     




    ?>
于 2013-11-06T17:57:08.340 回答