我可以通过基于文本框中的值与表中的列中的值之间的匹配的表单向 MySQL 表添加值吗?

发布于 2024-12-11 09:13:20 字数 1068 浏览 0 评论 0原文

我有一个包含许多列的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声无音无过去 2024-12-18 09:13:20

这应该可以帮助您开始。

//Get the value from the textbox
$name = mysql_real_escape_string($_POST['name']);
//Select all rows with the name
$query = "SELECT other_info FROM table1 WHERE name = '$name' ";
$result = mysql_query($query);
if ($result) {
  $rowcount = mysql_num_rows($result);
  //No rows found
  if ($rowcount == 0) {echo "no bookings found"; }
  else {
    //process the data.
    $row = mysql_fetch_row($result);
    $other_info = $row['other_info'];
    echo "other_info = ".hmtlentities($other_info);

请参阅此处: http://php.net/manual/en/book.mysql.php< /a>

代码注释

$row_count = count($_POST['name']);
if ($row_count > 0) {

  mysql_select_db($database, $connection);
  //$values = array(); 
  $name = array();
  $workshop = array()
  $replace = array()

  for($i = 0; $i < $row_count; $i++) {
    // variable sanitation...
    $name[i] = mysql_real_escape_string(ucwords($_POST['name'][$i]));
    $workshop[i] = mysql_real_escape_string($_POST['workshop'][$i]);
    //if you use option 2
    $replace[i] = "('".$name[i]."','".$workshop[i]."')";
  }
  $names = "'".implode("','",$name)."'";
  $query = "SELECT 1 FROM conference WHERE Name IN $names "; 
  $result = mysql_query($query);

  if ($result) {
    $rowcount = mysql_num_rows($result);

    if ($rowcount == 0) {
      echo "<HTMLCODE HERE>"."no bookings found"."<MORE HTML>"; 
    //OPTION 1
    } else {
      for($i = 0; $i < $row_count; $i++) {
        //$row = mysql_fetch_row($result);  
        $sql = "UPDATE conference SET Workshop = '$workshop[i]' 
                WHERE Name LIKE '$name[i]'";
        mysql_query($sql);
      }
    }
    //OPTION2
    } else {
      $replacement = implode(",",$replace);
      $sql = "REPLACE INTO conference (names, workshop) VALUES $replacement "
      mysql_query($sql);
    }
  }
}

This should get you started.

//Get the value from the textbox
$name = mysql_real_escape_string($_POST['name']);
//Select all rows with the name
$query = "SELECT other_info FROM table1 WHERE name = '$name' ";
$result = mysql_query($query);
if ($result) {
  $rowcount = mysql_num_rows($result);
  //No rows found
  if ($rowcount == 0) {echo "no bookings found"; }
  else {
    //process the data.
    $row = mysql_fetch_row($result);
    $other_info = $row['other_info'];
    echo "other_info = ".hmtlentities($other_info);

See here: http://php.net/manual/en/book.mysql.php

Comments on the code

$row_count = count($_POST['name']);
if ($row_count > 0) {

  mysql_select_db($database, $connection);
  //$values = array(); 
  $name = array();
  $workshop = array()
  $replace = array()

  for($i = 0; $i < $row_count; $i++) {
    // variable sanitation...
    $name[i] = mysql_real_escape_string(ucwords($_POST['name'][$i]));
    $workshop[i] = mysql_real_escape_string($_POST['workshop'][$i]);
    //if you use option 2
    $replace[i] = "('".$name[i]."','".$workshop[i]."')";
  }
  $names = "'".implode("','",$name)."'";
  $query = "SELECT 1 FROM conference WHERE Name IN $names "; 
  $result = mysql_query($query);

  if ($result) {
    $rowcount = mysql_num_rows($result);

    if ($rowcount == 0) {
      echo "<HTMLCODE HERE>"."no bookings found"."<MORE HTML>"; 
    //OPTION 1
    } else {
      for($i = 0; $i < $row_count; $i++) {
        //$row = mysql_fetch_row($result);  
        $sql = "UPDATE conference SET Workshop = '$workshop[i]' 
                WHERE Name LIKE '$name[i]'";
        mysql_query($sql);
      }
    }
    //OPTION2
    } else {
      $replacement = implode(",",$replace);
      $sql = "REPLACE INTO conference (names, workshop) VALUES $replacement "
      mysql_query($sql);
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文