【问题标题】:PHP not inserting to MYSQL tablesPHP没有插入MYSQL表
【发布时间】:2012-06-14 13:40:33
【问题描述】:

我有一个 html 表单、一个 php 脚本和一个 mysql 数据库我希望表单通过 php 脚本发布到 mysql 数据库。我填写表格并提交,但表格保持不变。我在 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 表

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

我不知道我做错了什么!请帮忙。

【问题讨论】:

  • 请不要将mysql_* 函数用于新代码。它们不再维护,社区已经开始deprecation process。看到red box?相反,您应该了解prepared statements 并使用PDOMySQLi。如果您不能决定,this article 将帮助您选择。如果你想学习,here is good PDO tutorial.
  • @Truth,完全会窃取您的评论以备将来使用!
  • 好吧,我不知道,我 8 个月前刚学过 MYSQL……你知道当你的“首选”解决方案现在被淘汰并变得“过时”时,你会偷懒,哈哈,我会的赶上 mysqli 但同时你知道这是怎么回事吗?
  • date这样的保留字命名列是个坏主意
  • @JohnConde:所有功劳都归功于 PHP 聊天中的 teresko。欢迎你加入:)

标签: php mysql apache ubuntu-11.10


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在您的 INSERT 语句中,int 值不需要引号。

    从现在开始,请提供错误消息(如果您没有看到,请在 apache 错误日志中查找)

    【讨论】:

      【解决方案3】:

      尝试在查询中指定列:

      $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; 
      

      【讨论】:

        【解决方案4】:

        $_POST['image'] 是什么意思?是你上传的图片吗?如果是这样,我确定您在表单属性上使用 enctype = multipart/form-data,并且您还使用 $_FILE['image']['name'] 来获取图像的名称。

        还有一点,你应该使用mysql_real_escape_string($_POST['string'])来防止mysql注入。并且不要在整数值之间使用引号。

        【讨论】:

        • 至于 $_POST['image'] 我只是输入图片的网址。
        猜你喜欢
        • 2016-07-08
        • 2013-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-01
        • 2019-09-21
        相关资源
        最近更新 更多