我可以通过基于文本框中的值与表中的列中的值之间的匹配的表单向 MySQL 表添加值吗?
我有一个包含许多列的 MySQL 表,其中包括一个名为“Name”的列。
我想为一个由两个文本框 A 和 B 组成的网页设计一个表单。在文本框 A 中,用户需要输入他们的姓名,在文本框 B 中输入一些其他信息。
我希望 PHP 脚本检查文本框 A 中的名称是否与 MySQL 表中名称列中已有的值匹配,如果匹配,则将文本框 B 中的值添加到该表中的另一列。如果未找到该名称,我希望脚本返回错误,类似于“我们的数据库中未找到您的预订”。
是否可以使用 PHP/MySQL 来完成此操作?如果可以,我将如何处理?
当前代码
$row_count = count($_POST['name']);
mysql_select_db($database, $connection);
if ($row_count > 0) {
$values = array();
for($i = 0; $i < $row_count; $i++) {
// variable sanitation...
$name = mysql_real_escape_string(ucwords($_POST['name'][$i]));
$workshop = mysql_real_escape_string($_POST['workshop'][$i]);
$query = "SELECT * FROM conference WHERE Name = '$name' ";
$result = mysql_query($query);
if ($result) {
$rowcount = mysql_num_rows($result);
if ($rowcount == 0) {echo "no bookings found"; }
else {
$row = mysql_fetch_row($result);
$sql = "UPDATE conference SET Workshop = '$workshop' WHERE Name = '$name'";
mysql_query($sql);
}
}
}
}
I have a MySQL table with a number of columns, including one called 'Name'.
I would like to design a form for a webpage which consists of two textboxes, A and B. In textbox A the user would be required to enter their name, and in textbox B some other information.
I would like the PHP script to check if the Name in Textbox A matches a value already in the Name column in the MySQL table, and if it does, add the value in Textbox B to another column in this table. If the name is not found, I would like the script to return an error, along the lines of "Your booking was not found on our database".
Is it possible to do this using PHP/MySQL and if it is, how would I go about this?
CURRENT CODE
$row_count = count($_POST['name']);
mysql_select_db($database, $connection);
if ($row_count > 0) {
$values = array();
for($i = 0; $i < $row_count; $i++) {
// variable sanitation...
$name = mysql_real_escape_string(ucwords($_POST['name'][$i]));
$workshop = mysql_real_escape_string($_POST['workshop'][$i]);
$query = "SELECT * FROM conference WHERE Name = '$name' ";
$result = mysql_query($query);
if ($result) {
$rowcount = mysql_num_rows($result);
if ($rowcount == 0) {echo "no bookings found"; }
else {
$row = mysql_fetch_row($result);
$sql = "UPDATE conference SET Workshop = '$workshop' WHERE Name = '$name'";
mysql_query($sql);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以帮助您开始。
请参阅此处: http://php.net/manual/en/book.mysql.php< /a>
代码注释
This should get you started.
See here: http://php.net/manual/en/book.mysql.php
Comments on the code