PHP脚本无法插入数据库

时间:2014-11-23 07:11:13

标签: 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>    `

2 个答案:

答案 0 :(得分:0)

用于连接数据库的密码由您提供是否正确?

对于localhost,我们始终使用blank密码。如果您提供的凭据是正确的。然后启用 PHP错误 mysql错误

Php错误:

ini_set('display_errors', True);

Mysql错误:

mysql_error();

答案 1 :(得分:0)

这会有效.. 这是行不通的,因为你有一个更新&#39;列是保留字,所以你必须像这样放

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