来自 PHP 表单的数据未发布到 mySQL [英] Data from PHP form is not posting to mySQL

查看:49
本文介绍了来自 PHP 表单的数据未发布到 mySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a novice with coding, so I am unsure why I am not getting data to store in mySQL after submitting a form. I started with a code generator, but it just ins't working. Thanks for any help. Here is my code:

表格:

<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();
?>

推荐答案

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

Here is the complete example of your code by using 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 语句中使用不带单引号或双引号的值进行绑定.

Prepared statements is used to clean your input for prevent SQL injection. You can use your value without single or double quotation in SQL statement for binding.

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

In Execute function you can pass an array for your SQL statement with same index name.

使用 PDO 或 mysqli_* 而不是 mysql_ 的原因是什么:因为 mysql_ 扩展已弃用且在 PHP 7 中不可用.

What is the reason to use PDO or mysqli_* instead of mysql_: Because mysql_ extension is deprecated and not available in PHP 7.

旁注:

我知道@Rahautos 已经提供了使用 MYSQL 标准 Date Format ('Ym-d') 的解决方案,就像我在我的执行.

I know @Rahautos already provided the solution to use MYSQL standard Date Format ('Y-m-d') as i used in my execution.

这篇关于来自 PHP 表单的数据未发布到 mySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆