2013-11-29 102 views
0

這是我的代碼:爲什麼不更新到數據庫?

<?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> 

在我的數據庫,我不能更新表。我能夠正確提取變量並回顯它們,但它不起作用。

我在過去'沒有選擇數據庫'時收到以下錯誤。

+0

你是否填寫前4個變量的有效憑證? $ host,$ username,$ password和$ tbl_name? –

+0

你可以給我發送錯誤嗎? – vs7

+0

是的,憑證是完全正常的,有沒有錯誤顯示奇怪... 基本上我運行php和沒有錯誤代碼後,我提交。 如果你複製並粘貼到一個PHP文件,填寫憑證,並有一個列id和名稱與幾行的表,那麼你會明白我的意思,沒有什麼 –

回答

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> 
0

再試一次,但沒有圍繞數據庫連接變量的引號。我的意思是,他們是變量&不是字符串,對吧?

原件報價:

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

清理不帶引號:

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

完成!雖然沒有對我的問題產生影響:( –

0

你應該改變你的代碼中加入下面的代碼片段。這樣你可以更好地調試你的代碼:

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

$result->close(); 
+0

我做了你所說的,但我不明白,我把它放在哪裏? –

1

我覺得你忘了選擇數據庫。試試你的連接之後,把這個:

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

如果發生這種情況,請仔細檢查姓名,權限,等等。在我的課

相關問題