PHP脚本可以';t插入数据库
它一直告诉我它无法插入数据库。 有人能概述一下我做错了什么吗?到现在为止,我从来没有遇到过任何问题PHP脚本可以';t插入数据库,php,Php,它一直告诉我它无法插入数据库。 有人能概述一下我做错了什么吗?到现在为止,我从来没有遇到过任何问题 <html> <title>Server logs</title> <style type="text/css"> body{ background-color:#fff; font-family:tahoma; } </style> <body> <center> <h3>Server 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="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> `
服务器日志
身体{
背景色:#fff;
字体系列:tahoma;
}
服务器更新
您提供的连接数据库的密码正确吗
对于localhost,我们总是使用空白
密码。如果您提供的凭据是正确的。然后启用PHP错误和mysql错误
Php错误:
ini_set('display_errors', True);
mysql_error();
Mysql错误:
ini_set('display_errors', True);
mysql_error();
这会有用的。。
这不起作用,因为您有一个“更新”列,它是一个保留字,所以您必须这样放置
`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>
服务器日志
身体{
背景色:#fff;
字体系列:tahoma;
}
服务器更新
检查mysql\u error
:此方法有效=),因为我在发布之前测试了此方法