2014-11-23 62 views
0

它一直告訴我它無法插入到數據庫中。 有人可以概述我做錯了什麼嗎?直到現在,我從未遇到任何問題。PHP腳本無法插入到數據庫中

<html> 
<title>Server logs</title> 
<style type="text/css"> 
body{ 
background-color:#fff; 
font-family:tahoma; 
} 
</style> 
<body> 
<center> 
<h3>Server updates</h3> 
<?php 
//variables for connecting. 
$host="localhost"; 
$user="root"; 
$pass="aion"; 
$db="website"; 
//connection to the server. 
$con=mysql_connect($host, $user, $pass); 
if($con) 
{ 
echo "Connection: <font color='lime'>Connected to the database</font><br>"; 
} 
else{ 
echo "Connection: <font color='red'>Couldn't connect to the database</font><br>"; 
} 
$select=mysql_select_db($db); 
if($select) 
{ 
echo "Db status: <font color='lime'>Selected the db successfully</font>"; 
} 
else{ 
echo "Db status: <font color='red'>couldn't select the database</font>"; 
} 
?> 
<br><br> 
<form action="" method="post"> 
<table width="300" style="text-align:center;"> 
<tr> 
<td>Username: </td> 
<td><input type="text" name="username" placeholder="Enter your name here"></td> 
</tr> 
<tr> 
<td>Update: </td> 
<td><input type="text" name="update" placeholder="Enter update information here"  style="width:300px;"></td> 
</tr> 
<tr> 
<td></td> 
<td><input type="submit" name="submit" value="Add"></td> 
</tr> 
</table> 
</form> 
<p>This was made so we can keep track of the updates we do to the server.</p> 
<?php 
if(isset($_POST['submit'])) 
{ 
if(!empty($_POST['username'] && !empty($_POST['update']))) 
{ 
//the information that we passed from the 2 textboxes 
$name=$_POST['username']; 
$update=$_POST['update']; 
//variables for connecting. 
$host="localhost"; 
$user="root"; 
$pass="aion"; 
$db="website"; 
//connection to the server. 
$con=mysql_connect($host, $user, $pass); 
mysql_select_db($db); 
//running a query to insert the previous information gathered into the database. 
$sql=mysql_query("INSERT INTO logs (name,update) VALUES ('$name','$update') "); 
//running an if statement to see if the info was inserted, if it was it will display that it was  inserted else display that it wasn't. 
if($sql) 
{ 
echo "Successfully inserted the update"; 
} 
else{ 
echo "There was an error inserting the info into the database"; 
} 
} 
else 
{ 
echo "no information was entered! Please go back and input some information"; 
} 
} 

?> 
</body> 
</html> ` 
+0

檢查'mysql_error':http://php.net/manual/en/function.mysql-error.php – danronmoon 2014-11-23 07:24:04

回答

0

用於建立與數據庫的連接的密碼由您給出是正確的?

對於本地主機我們總是使用blank密碼。如果證書是由你給出的是正確的。然後啓用PHP錯誤mysql錯誤

PHP錯誤:

ini_set('display_errors', True); 

Mysql的錯誤:

mysql_error(); 
0

這將工作.. 不工作,因爲你有一個 '更新' 列這是一個保留所以你必須這樣寫

`INSERT INTO logs (name,`update`) VALUES ('$name','$update')` 

和你的一些代碼已經失蹤括號

<html> 
    <title>Server logs</title> 
    <style type="text/css"> 
    body{ 
    background-color:#fff; 
    font-family:tahoma; 
    } 
    </style> 
    <body> 
    <center> 
    <h3>Server updates</h3> 
    <?php 
    //variables for connecting. 
$host="localhost"; 
$user="root"; 
$pass="aion"; 
$db="website"; 
    //connection to the server. 
    $con=mysql_connect($host, $user, $pass); 
    if($con) 
    { 
    echo "Connection: <font color='lime'>Connected to the database</font><br>"; 
    } 
    else{ 
    echo "Connection: <font color='red'>Couldn't connect to the database</font><br>"; 
    } 

    $select=mysql_select_db($db); 
    if($select) 
    { 
    echo "Db status: <font color='lime'>Selected the db successfully</font>"; 
    } 
    else{ 
    echo "Db status: <font color='red'>couldn't select the database</font>"; 
    } 
    ?> 
    <br><br> 
    <form action="" method="post"> 
    <table width="300" style="text-align:center;"> 
    <tr> 
    <td>Username: </td> 
    <td><input type="text" name="username" placeholder="Enter your name here"></td> 
    </tr> 
    <tr> 
    <td>Update: </td> 
    <td><input type="text" name="update" placeholder="Enter update information here"  style="width:300px;"></td> 
    </tr> 
    <tr> 
    <td></td> 
    <td><input type="submit" name="submit" value="Add"></td> 
    </tr> 
    </table> 
    </form> 
    <p>This was made so we can keep track of the updates we do to the server.</p> 
    <?php 
    if(isset($_POST['submit'])) 
    { 
    if(!empty($_POST['username']) && !empty($_POST['update'])) 
    { 
    //the information that we passed from the 2 textboxes 
    $name=$_POST['username']; 
    $update=$_POST['update']; 
    //variables for connecting. 
    $host="localhost"; 
    $user="root"; 
    $pass=""; 
    $db="website"; 
    //connection to the server. 
    $con=mysql_connect($host, $user, $pass); 
    mysql_select_db($db); 
    //running a query to insert the previous information gathered into the database. 
    $sql=mysql_query("INSERT INTO logs (name,`update`) VALUES ('$name','$update') "); 
    //running an if statement to see if the info was inserted, if it was it will display that it was  inserted else display that it wasn't. 
    if($sql) 
    { 
    echo "Successfully inserted the update"; 
    } 
    else{ 
    echo "There was an error inserting the info into the database"; 
    } 
    } 
    else 
    { 
    echo "no information was entered! Please go back and input some information"; 
    } 
    } 

    ?> 
    </body> 
    </html> 
+0

這個工程=),因爲我這個測試在我發佈之前.. =)) – jmn 2014-11-23 07:50:32