为什么不更新到数据库?

时间:2013-11-29 05:50:45

标签: php mysql

这是我的代码:

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="blah"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="SubCategories"; // Table name 

$con=mysqli_connect("$host", "$username", "$password", "$db_name");

if (mysqli_connect_errno()) // Check connection
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" /> 
        <title></title>
    </head>
    <body>
    <form action="untitled.php" method="post"><!-- untitled.php -->

<?php
  //print_r($_POST); //print all checked elements
  //echo "<br>".$email, $_POST["update"][$i];
  //mysql_real_escape_string ($route )

if(isset($_POST['submit'])) {
    foreach ($_POST["holder"] as $i=>$email) { 
        $y=$email;
        $h=$_POST["update"][$i];
        $res2=mysqli_query("UPDATE ".$tbl_name." SET subCat2 = '" . $y . "' WHERE id =". $h,$con);
        if ($res2){

        }
        else{
            echo "<h1>NOT WORKING!</h1>";
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }       
    }

}

$result = mysqli_query($con,"SELECT * FROM $tbl_name");

echo "<br>";
while($row = mysqli_fetch_array($result))
  {
    echo '<input type="text" name="holder[]" id="checkbox-1" class="custom" value=" ' . $row['subCat2'] . '"/>';
    echo '<input type="hidden" name="update[]" id="checkbox-1" class="custom" value=" ' . $row['subCatNum'] . '"/>';
    echo "<br>";
  }
?>
</br>
        <input type="submit" name="submit">
    </form>

    </body>
</html>

我无法更新数据库中的表格。我能够正确地提取变量并回显它们,但它不起作用。

我在过去'没有选择数据库'时遇到以下错误。

4 个答案:

答案 0 :(得分:1)

我认为您忘了选择数据库。尝试在连接后输入:

if (!mysqli_select_db($con, $db_name)) {
    die("Uh oh, couldn't select database $db_name");
}

如果发生这种情况,请仔细检查名称,权限等。

答案 1 :(得分:0)

再试一次,但没有围绕数据库连接变量的引号。我的意思是,它们是变量&amp;不是字符串,对吧?

带引号的原文:

$con=mysqli_connect("$host","$username","$password","$db_name");

不加引号清理:

$con=mysqli_connect($host,$username,$password,$db_name);

答案 2 :(得分:0)

您应该更改下面添加代码段的代码。这样您就可以更好地调试代码:

if (!$result = $mysqli->query("YOUR-SQL", MYSQLI_USE_RESULT)) {
     printf("Error: %s\n", $mysqli->error);
}
...do something here..

$result->close();

答案 3 :(得分:0)

我班上有人帮我解决了,谢谢!这是代码,非常精彩:)

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="blah"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql"; // Table name 

$con=mysqli_connect($host,$username,$password,$db_name);

if (mysqli_connect_errno()) // Check connection
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


?>

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" /> 
        <title></title>
    </head>
    <body>
    <form action="untitled.php" method="post"><!-- untitled.php -->

<?php

if(isset($_POST['submit'])) {
    foreach ($_POST["holder"] as $i=>$email) { 
        $y=$email;
        $h=$_POST["update"][$i];
        $sql2="UPDATE ".$tbl_name." SET name = '" . $y . "' WHERE id =". $h;
        //$res2=mysqli_query("UPDATE ".$tbl_name." SET name = '" . $y . "' WHERE id =". $h,$con);
        $res2=mysqli_query($con,$sql2);
        if ($res2){

        }
        else{
            echo "<h1>NOPE!</h1>";
            print "Failed to connect to MySQL: " . mysqli_error();

        }       
    }

}



$result = mysqli_query($con,"SELECT * FROM ".$tbl_name);



echo "<br>";
while($row = mysqli_fetch_array($result))
  {
    echo '<input type="text" name="holder[]" id="checkbox-1" class="custom" value=" ' . $row['name'] . '"/>';
    echo '<input type="hidden" name="update[]" id="checkbox-1" class="custom" value=" ' . $row['id'] . '"/>';
    //echo '<input type="text" class="a" name="holder2[]" id="checkbox-1" class="custom" value="' . $row['price'] . '" />';
    echo "<br>";
  }


?>

        </br>
        <input type="submit" name="submit">
    </form>

    </body>
</html>