2014-11-23 2 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

ответ

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

Смежные вопросы