我有一个用户注册 MySql 数据库,我希望用户使用表单输入数据。该表有一列我已预先填充了唯一代码。这个想法是每个用户在注册时都会获得一个唯一的代码。
首先,有没有比我的设置更好的方法来处理这个问题? 其次,如果没有更好的方法,如何为每次表单提交只更新一行数据?
这是我目前正在做的事情:
登记表
<form action="confirm.php" method="post" onsubmit="return Validate();">
<p>First Name: <input type="text" name="firstname" /></p>
<p>Last Name: <input type="text" name="lastname" /></p>
<p>Address: <input type="text" name="address" /></p>
<p>City: <input type="text" name="city" /></p>
<p>State: <input type="text" name="state" /></p>
<p>Zip Code: <input type="text" name="zipcode" /></p>
<p>Phone Number: <input type="text" name="phone" /></p>
<p>Email: <input type="text" name="email" /></p>
<p><input type="submit" /></p>
</form>
确认.php
<form action="register.php" method="post" onsubmit="return Validate();">
<p>First Name: <input type="text" name="firstname" id="firstname" size="36" value="<?php echo $_POST['firstname']; ?>"> </p>
<p>Last Name: <input type="text" name="lastname" id="lastname" size="36" value="<?php echo $_POST['lastname']; ?>"> </p>
<p>Address: <input type="text" name="address" id="address" size="36" value="<?php echo $_POST['address']; ?>"> </p>
<p>City: <input type="text" name="city" id="city" size="36" value="<?php echo $_POST['city']; ?>"> </p>
<p>State: <input type="text" name="state" id="state" size="36" value="<?php echo $_POST['state']; ?>"> </p>
<p>Zip Code: <input type="text" name="zipcode" id="zipcode" size="36" value="<?php echo $_POST['zipcode']; ?>"> </p>
<p>Phone Number: <input type="text" name="phone" id="phone" size="36" value="<?php echo $_POST['phone']; ?>"> </p>
<p>Email: <input type="text" name="email" id="email" size="36" value="<?php echo $_POST['email']; ?>"> </p>
<input type="hidden" name="redirect_values" value="true">
<input type="submit" name="confirm" value="Confirm Information">
<input type="button" name="return" value="Change Information" onClick="javascript: window.history.back(-1)";>
</form>
注册.php
<?php ob_start();
$url = 'registercomplete.php';
require("register_dbinfo.php");
$con=mysqli_connect($host,$username,$password,$database);
if (mysqli_connect_errno()) {echo "Failed to connect to MySQL: ".mysqli_connect_error();}
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$address = mysqli_real_escape_string($con, $_POST['address']);
$city = mysqli_real_escape_string($con, $_POST['city']);
$state = mysqli_real_escape_string($con, $_POST['state']);
$zipcode = mysqli_real_escape_string($con, $_POST['zipcode']);
$phone = mysqli_real_escape_string($con, $_POST['phone']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$sql="INSERT INTO users (id, firstname, lastname, address, city, state, zipcode, phone, email)VALUES ('','$firstname', '$lastname', '$address', '$city', '$state', '$zipcode', '$phone', '$email')";
if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));}
mysqli_close($con);
while (ob_get_status())
{ob_end_clean();}
header( "Location: $url" );
?>
数据库列:
id|促销|门票|名字|姓氏|地址|城市|州|邮政编码|电话|电子邮件
最佳答案
我不确定我是否理解了您的问题。
如果 id
为 INT,则使用 A_I(自动增量)使属性UNIQUE。
如果您需要一个唯一的promo
字符串,您可能需要使用哈希函数(如 SHA-256)以避免字符串冲突。
除了你的问题之外,我建议你不要连接转义字符串,因为这种技术不会阻止 SQL 注入(inject)。您应该使用 prepared statements and stored procedures .
关于php - 当数据库已经包含数据列时,如何从表单将数据输入到mysql数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30175564/