简体   繁体   English

PHP未插入MYSQL表

[英]PHP not inserting to MYSQL tables

I have a html form, a php script and a mysql database I want the form to post to the mysql database through the php script. 我有一个html表单,一个php脚本和一个mysql数据库,我希望该表单通过php脚本发布到mysql数据库。 i fill out the form and submit but the table stays the same. 我填写表格并提交,但表格保持不变。 I'm using Lamp setup with Ubuntu. 我正在Ubuntu下使用Lamp安装程序。

/var/www/add_review.php
<?
$username="user";
$password="password";
$database="database";
$review=$_POST['review'];
$Cname=$_POST['Cname'];
$picture=$_POST['picture'];
$profile=$_POST['Cprofile'];
$location=$_POST['location'];
$ratingImg=$_POST['ratingImg'];
$rating=$_POST['rating'];
$date=$_POST['date'];
$Creview=$_POST['Creview'];
$link=$_POST['link'];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO table VALUES ('$review','$Cname','$picture','$location','$ratingImg','$rating','$date','$Creview','$link')";
mysql_query($query);
if($query)
{
    echo "Success!";
}
else
{
    die(mysql_error());
}
mysql_close();
echo "Review Added!";
echo "<br />";
echo $review;
echo "<br />";
echo $name;
echo "<br />";
echo $picture;
echo "<br />";
echo $profile;
echo "<br />";
echo $location;
echo "<br />";
echo $ratingImg;
echo "<br />";
echo $rating;
echo "<br />";
echo $date;
echo "<br />";
echo $Creview;
echo "<br />";
echo $link;
?>

/var/www/add_review.html
<h1>Add A Drink</h1>
<form action="add_review.php" method="post">
<p>Review # <input type="text" name="review"><br></p>
<p>UserName <input type="text" name="Cname"><br></p>
<p>Picture URL <input type="text" name = "picture"><br></p>
<p>Users Profile URL <input type="text" name = "Cprofile"><br></p>
<p>Location <input type="text" name = "location"><br></p>
<p>Star URL <input type="text" name = "ratingImg"><br></p>
<p>Star Value <input type="text" name = "rating"><br></p>
<p>Date(MMDDYYYY) <input type="text" name = "date"><br></p>
<p>Users Review<br> <textarea name="Creview"></textarea><br></p>
<p>Review Link <input type="text" name = "link"><br></p>
<input type="submit" value="Submit">
</form>

MYSQL Table MYSQL表

+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| review    | int(11)     | YES  |     | NULL    |       |
| Cname     | varchar(20) | YES  |     | NULL    |       |
| picture   | text        | YES  |     | NULL    |       |
| Cprofile  | text        | YES  |     | NULL    |       |
| location  | varchar(30) | YES  |     | NULL    |       |
| ratingImg | text        | YES  |     | NULL    |       |
| rating    | float       | YES  |     | NULL    |       |
| date      | int(11)     | YES  |     | NULL    |       |
| Creview   | text        | YES  |     | NULL    |       |
| link      | text        | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

I can't figure out what I'm doing wrong! 我不知道自己在做什么错! please help. 请帮忙。

您似乎没有为CProfile提供值,这可能会导致某种错误。

In your INSERT sentence, the int values does not require quotes. 在您的INSERT语句中,int值不需要引号。

From now on, please supply error messages (if you don't see one, look for it on apache error log) 从现在开始,请提供错误消息(如果看不到,请在apache错误日志中查找)

try specifying your columns in your query: 尝试在查询中指定列:

$query = <<<EOQ

INSERT INTO yelpreviews(review, Cname, picture, location, 
                        ratingImg, rating, `date`, Creview, link)
VALUES('$review','$Cname','$picture','$location',
       '$ratingImg','$rating','$date','$Creview','$link')

EOQ; 

What do you mean $_POST['image'] ? 你是什​​么意思$_POST['image'] Is that you're uploading image? 您是要上传图片吗? If so, i'm sure you use the enctype = multipart/form-data on the form attribute, and also you use $_FILE['image']['name'] to get the name of the image. 如果是这样,我确定您在form属性上使用了enctype = multipart / form-data,并且还使用了$ _FILE ['image'] ['name']来获取图像的名称。

One more thing, you should use the mysql_real_escape_string($_POST['string']) to prevent mysql injection. 还有一件事,您应该使用mysql_real_escape_string($_POST['string'])防止mysql注入。 And also do not use quote between integer values. 并且也不要在整数值之间使用引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM