gpt4 book ai didi

php - 如何将用户 php 帖子的输入与 MySQL 进行比较

转载 作者:行者123 更新时间:2023-11-29 18:46:43 26 4
gpt4 key购买 nike

我正在自学 php 和 MySQL,现在我在 MySQL 方面遇到了问题。

我想将用户输入的电话号码与MYSQL中的电话号码进行比较,如果是MYSQL中的则不再注册。

我的代码:

<?php

require_once 'connection/connection.php';

// Variables from HTML to php
$worker_Name = $_POST['workerNameFromHtml']; // worker Name
$worker_City = $_POST['workerCityFromHtml']; // workerCity
$worker_career = $_POST['workerCareerFromHtml']; // worker career
$worker_PhoneNumber = $_POST['workerPhonNumberFromHtml']; // worker Phone Number
$worker_SecondPhoneNumber = $_POST['workerSecondPhoneNumberFromHtml']; // worker Second Phone Number
$submt=$_POST['submitFromHtml'];

if($submt){

$qry = ( "SELECT workrPhoneNumber FROM workersTable WHERE workrPhoneNumber = '$worker_PhoneNumber'") or die(mysql_error());
$result = $connect->query($qry);
$num = $result->num_rows;

if ($num == 1) {
$here = "INSERT INTO workersTable VALUES('','$worker_Name','$worker_City','$worker_career','$worker_PhoneNumber','$worker_SecondPhoneNumber')";
$query = $connect->query($here);
print "Successfully added!";
}
else {print "This number has already been entered Thank you for your cooperation!";}}

$connect->close();

到目前为止我还没有找到解决这个问题的方法。

最佳答案

这里最大的问题是你试图在字符串中包含变量。

"SELECT workrPhoneNumber FROM workersTable WHERE workrPhoneNumber = '$worker_PhoneNumber'"

如果您想这样做,您需要将变量与字符串连接起来。

"SELECT workrPhoneNumber FROM workersTable WHERE workrPhoneNumber = '".$worker_PhoneNumber."'"

请记住,如果您这样做,您将需要首先清理变量以防止 SQL 注入(inject)。另外,当您插入变量时,您实际上会想要使用这样的准备好的语句:

"INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)"

其中第一组值是数据库中列的名称,第二组是您要放入其中的 PHP 变量。

关于php - 如何将用户 php 帖子的输入与 MySQL 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44572435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com