mySQL php 表单未保存数据
mySQL php form not saving data
我正在尝试使用以下 php 表单,但每次收到错误消息时 保存失败请打印数据...
我认为这意味着它可以正常连接到数据库,但是数据没有正确地到达 php 文件或数据库。任何指针都会很棒...
谢谢
亨利
new_client.html形式
<form id="form_956123" class="appnitro" method="post" action="new_client.php">
<div class="form_description">
<h2>New Client</h2>
<p>Please enter your client details below</p>
</div>
<ul >
<li id="li_1" >
<label class="description" for="firstname">First Name </label>
<div>
<input id="firstname" name="firstname" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_2" >
<label class="description" for="lastname">Last Name </label>
<div>
<input id="lastname" name="lastname" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_3" >
<label class="description" for="address">Address </label>
<div>
<textarea id="address" name="address" class="element textarea medium"></textarea>
</div>
</li> <li id="li_4" >
<label class="description" for="postcode">Postcode </label>
<div>
<input id="postcode" name="postcode" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_5" >
<label class="description" for="email">Email </label>
<div>
<input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_6" >
<label class="description" for="tel">Phone </label>
<div>
<input id="tel" name="tel" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_7" >
<label class="description" for="dob">Date of Birth </label>
<div>
<input id="dob" name="dob" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="956123" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
new_client.php
<?php //Post Params
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$tel = $_POST['tel'];
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt = $conn->prepare("INSERT INTO client_data (date, userid, firstname, lastname, address, postcode, email, dob, tel) VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssssss",$firstname,$firstname,$lastname,$address,$postcode,$email,$dob,$tel);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
?>
TABLE 格式
Column Type
date date
userid text
firstname text
lastname text
address text
postcode text
email text
dob text
tel text
$query = " INSERT INTO client_data ( date, userid, firstname, lastname, address, postcode, email, dob, tel ) VALUES ( 'now()','','$firstname', '$firstname', '$lastname', '$address', '$postcode', '$email', '$dob', '$tel' ) ";
或者试试这个
$query = " INSERT INTO client_data ( date, firstname, lastname, address, postcode, email, dob, tel ) VALUES ( 'now()','$firstname', '$firstname', '$lastname', '$address', '$postcode', '$email', '$dob', '$tel' ) ";
我假定用户标识字段是自动递增的。
然后更改此行
$mysql->query($query);
到
$mysqli->query($query);
您似乎正在使用 mysql 进行连接并使用 mysqli 进行查询,但未指定数据库连接。
尝试:
$result = mysli_query($conn, $query);
错误在这里:
mysql_query($query)
首先,您不能混合使用 mysql_
和 mysqli_
。其次,如果您使用过程式,则必须在第一个参数中指定连接(即 mysqli_query($conn, $query)
,或 OO 风格:$conn->query
)。
但是,由于您正在处理字符串,因此需要使用准备好的语句来避免 SQL 注入。正确的做法是:
$stmt = $conn->prepare("INSERT INTO client_data ( date, userid, firstname, lastname, address, postcode, email, dob, tel ) VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?, ? ) "));
$stmt->bind_param("ssssssss",$firstname,$firstname,$lastname,$address,$postcode,$email,$dob,$tel);
$stmt->execute();
顺便问一下,为什么 userid
不是 ID?
更新: 将您的代码更改为:
$stmt = $conn->prepare("INSERT INTO client_data ( date, userid, firstname, lastname, address, postcode, email, dob, tel ) VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?, ? ) "));
if ( false===$stmt ) { die('prepare() failed: ' . htmlspecialchars($mysqli->error)); }
$rc = $stmt->bind_param("ssssssss",$firstname,$firstname,$lastname,$address,$postcode,$email,$dob,$tel);
if ( false===$rc ) { die('bind_param() failed: ' . htmlspecialchars($stmt->error)); }
$rc = $stmt->execute();
if ( false===$rc ) { die('execute() failed: ' . htmlspecialchars($stmt->error)); }
然后告诉我你得到了什么输出。
换行
$result = mysql_query($query);
至
$result = mysqli_query($conn, $query);
或使用准备好的语句(推荐)。
然后,如果还是不行,换
echo 'Saving Failed Please Print Data...';
至
echo mysqli_error($conn);
暂时看看会不会说是什么错误
我正在尝试使用以下 php 表单,但每次收到错误消息时 保存失败请打印数据...
我认为这意味着它可以正常连接到数据库,但是数据没有正确地到达 php 文件或数据库。任何指针都会很棒...
谢谢
亨利
new_client.html形式
<form id="form_956123" class="appnitro" method="post" action="new_client.php">
<div class="form_description">
<h2>New Client</h2>
<p>Please enter your client details below</p>
</div>
<ul >
<li id="li_1" >
<label class="description" for="firstname">First Name </label>
<div>
<input id="firstname" name="firstname" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_2" >
<label class="description" for="lastname">Last Name </label>
<div>
<input id="lastname" name="lastname" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_3" >
<label class="description" for="address">Address </label>
<div>
<textarea id="address" name="address" class="element textarea medium"></textarea>
</div>
</li> <li id="li_4" >
<label class="description" for="postcode">Postcode </label>
<div>
<input id="postcode" name="postcode" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_5" >
<label class="description" for="email">Email </label>
<div>
<input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_6" >
<label class="description" for="tel">Phone </label>
<div>
<input id="tel" name="tel" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_7" >
<label class="description" for="dob">Date of Birth </label>
<div>
<input id="dob" name="dob" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="956123" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
new_client.php
<?php //Post Params
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$tel = $_POST['tel'];
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt = $conn->prepare("INSERT INTO client_data (date, userid, firstname, lastname, address, postcode, email, dob, tel) VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssssss",$firstname,$firstname,$lastname,$address,$postcode,$email,$dob,$tel);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
?>
TABLE 格式
Column Type
date date
userid text
firstname text
lastname text
address text
postcode text
email text
dob text
tel text
$query = " INSERT INTO client_data ( date, userid, firstname, lastname, address, postcode, email, dob, tel ) VALUES ( 'now()','','$firstname', '$firstname', '$lastname', '$address', '$postcode', '$email', '$dob', '$tel' ) ";
或者试试这个
$query = " INSERT INTO client_data ( date, firstname, lastname, address, postcode, email, dob, tel ) VALUES ( 'now()','$firstname', '$firstname', '$lastname', '$address', '$postcode', '$email', '$dob', '$tel' ) ";
我假定用户标识字段是自动递增的。
然后更改此行
$mysql->query($query);
到
$mysqli->query($query);
您似乎正在使用 mysql 进行连接并使用 mysqli 进行查询,但未指定数据库连接。
尝试:
$result = mysli_query($conn, $query);
错误在这里:
mysql_query($query)
首先,您不能混合使用 mysql_
和 mysqli_
。其次,如果您使用过程式,则必须在第一个参数中指定连接(即 mysqli_query($conn, $query)
,或 OO 风格:$conn->query
)。
但是,由于您正在处理字符串,因此需要使用准备好的语句来避免 SQL 注入。正确的做法是:
$stmt = $conn->prepare("INSERT INTO client_data ( date, userid, firstname, lastname, address, postcode, email, dob, tel ) VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?, ? ) "));
$stmt->bind_param("ssssssss",$firstname,$firstname,$lastname,$address,$postcode,$email,$dob,$tel);
$stmt->execute();
顺便问一下,为什么 userid
不是 ID?
更新: 将您的代码更改为:
$stmt = $conn->prepare("INSERT INTO client_data ( date, userid, firstname, lastname, address, postcode, email, dob, tel ) VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?, ? ) "));
if ( false===$stmt ) { die('prepare() failed: ' . htmlspecialchars($mysqli->error)); }
$rc = $stmt->bind_param("ssssssss",$firstname,$firstname,$lastname,$address,$postcode,$email,$dob,$tel);
if ( false===$rc ) { die('bind_param() failed: ' . htmlspecialchars($stmt->error)); }
$rc = $stmt->execute();
if ( false===$rc ) { die('execute() failed: ' . htmlspecialchars($stmt->error)); }
然后告诉我你得到了什么输出。
换行
$result = mysql_query($query);
至
$result = mysqli_query($conn, $query);
或使用准备好的语句(推荐)。
然后,如果还是不行,换
echo 'Saving Failed Please Print Data...';
至
echo mysqli_error($conn);
暂时看看会不会说是什么错误