【问题标题】:Data from PHP form is not posting to mySQL [closed]PHP 表单中的数据未发布到 mySQL [关闭]
【发布时间】:2016-01-14 04:27:44
【问题描述】:

我是编码新手,所以我不确定为什么在提交表单后我没有将数据存储在 mySQL 中。我从一个代码生成器开始,但它只是不起作用。谢谢你的帮助。这是我的代码:

表格:

<html>
  <body>
    <form id="FormName" action="added.php" method="post" name="FormName">
    <table width="448" border="0" cellspacing="2" cellpadding="0">

    <tr><td width = "150"><div align="right"><label for="name">Name of Farm    </label></div></td>

<td><input id="name" name="name" type="text" size="25" value="" maxlength="255"></td></tr><tr><td width = "150"><div align="right"><label for="owners">Name of Owners</label></div></td>

<td><input id="owners" name="owners" type="text" size="25" value="" maxlength="255"></td></tr><tr><td width = "150"><div align="right"><label for="location">Location (city,state)</label></div></td>

<td><input id="location" name="location" type="text" size="25" value="" maxlength="255"></td></tr><tr><td width = "150"><div align="right"><label for="phone">Phone</label></div></td>

<td><input id="phone" name="phone" type="text" size="25" value="" maxlength="10"></td></tr><tr><td width = "150"><div align="right"><label for="email">Email</label></div></td>

<td><input id="email" name="email" type="text" size="25" value="" maxlength="255"></td></tr><tr><td width = "150"><div align="right"><label for="website">Website</label></div></td>

<td><input id="website" name="website" type="text" size="25" value="" maxlength="255"></td></tr><tr><td width = "150"><div align="right"><label for="description">Description</label></div></td>

<td><textarea id="description" name="description" rows="4" cols="40"></textarea></td></tr><tr><td width = "150"><div align="right"><label for="dateadded">Today's Date</label></div></td>

<td><input id="dateadded" name="dateadded" type="text" size="25" value="" maxlength="255"></td></tr><tr><td width = "150"><div align="right"><label for="logo">Logo</label></div></td>

<td><input id="logo" name="logo" type="text" size="25" value="" maxlength="255"></td></tr><tr><td width = "150"><div align="right"><label for="state">State</label></div></td>

<td><input id="state" name="state" type="text" size="25" value="" maxlength="2"></td></tr><tr><td width="150"></td><td>

<input type="submit" name="submitButtonName" value="Add"></td>
</tr></table></form>
</body>
</html>

PHP is in 2 files - one with db connection instructions and the other to post the data in the mySQL.


**Code to Connect to mySQL:**

<?php

$hostname='localhost'; //// specify host, i.e. 'localhost'
$user='llamabre_visitor'; //// specify username
$pass='llama'; //// specify password
$dbase='llamabre_farms1'; //// specify database name
$connection = mysql_connect("$hostname" , "$user" , "$pass") 
or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection) or die ("Can't select database.");
?>

<a href="index.php">Back to List</a>


**Code for posting to mySQL:**

<?php

include("connect.php");

$name = trim($_POST['name']);
$owners = trim($_POST['owners']);
$location = trim($_POST['location']);
$phone = trim($_POST['phone']);
$email = trim($_POST['email']);
$website = trim($_POST['website']);
$description = trim($_POST['description']);
$dateadded = trim($_POST['dateadded']);
$logo = trim($_POST['logo']);
$state = trim($_POST['state']);

$query = "INSERT INTO farmsdir (id, name, owners, location, phone, email, website, description, dateadded, logo, state)

VALUES ('', '$name', '$owners', '$location', '$phone', '$email', '$website', '$description', '$dateadded', '$logo', '$state')";

$results = mysql_query($query);

if ($results)
{
  echo "Details added.";
}
mysql_close();
?>

【问题讨论】:

  • 糟糕的 HTML,为 SQL 注入开放,使用已弃用的 mysql_* 函数,没有检查连接状态。你最好先把这些修好。尝试使用 MySQLi / PDO。
  • 请不要使用mysql_select_db。您对 SQL 注入敞开心扉。请改用 PDO。查看这些 nettuts 教程以帮助您入门:herehere。它最终实际上是更少的代码,并创建了更整洁的代码。
  • "$var" 可以只是 $var 在您的 mysql_connect() 调用中不带引号。

标签: php mysql


【解决方案1】:

这是使用PDO 的完整代码示例:

$dbhost = "localhost";
$dbname = "llamabre_farms1";
$dbusername = "llamabre_visitor";
$dbpassword = "llama";

$link = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbusername,$dbpassword);

$statement = $link->prepare("
    INSERT INTO farmsdir 
    (name, owners, location, phone, email, website, description, dateadded, logo, state)
    VALUES(:fname, :sowners, :slocation, :sphone, :semail, 
    :swebsite, :sdescription, :dateadded, :logo, :state)
    ");

$statement->execute(array(
    "fname" => $_POST['name'],
    "sowners" => $_POST['owners'],
    "slocation" => $_POST['location'],
    "sphone" => $_POST['phone'],
    "semail" => $_POST['email'],
    "swebsite" => $_POST['website'],
    "sdescription" => $_POST['description'],
    "dateadded" => date('Y-m-d',strtotime($_POST['dateadded'])),
    "logo" => $_POST['logo'],
    "state" => $_POST['state'],
));

说明:

Prepared statements 用于清理您的输入以防止SQL 注入。 您可以在SQL 语句中使用不带单引号或双引号的值进行绑定。

Execute 函数中,您可以为SQL statement 传递一个具有相同索引名称的数组。

使用PDO或mysqli_*代替mysql_的原因是什么 因为 mysql_ 扩展名已被弃用且在 PHP 7 中不可用。

旁注:

我知道@Rahautos 已经提供了使用MYSQL 标准Date Format ('Y-m-d') 的解决方案,就像我在execution 中使用的那样。

【讨论】:

  • 谢谢!今晚我会多看看这个。我不知道 PDO 和 mysqli。
  • 当然你可以@jim ...如果你认为这个答案可以帮助你解决问题而不是点击左边的绿色勾选标记为接受这将有助于其他人
【解决方案2】:

使用strtotime 它会根据您提供的日期格式做出假设。比如

$dateadded=date("Y-m-d", strtotime($dateadded))

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 2016-03-24
    • 2012-06-28
    • 1970-01-01
    • 2014-09-15
    • 2016-11-02
    • 1970-01-01
    • 2016-05-06
    相关资源
    最近更新 更多