PHP我的sql文本框值与数据库字段值的比较

时间:2017-09-26 15:47:13

标签: php html mysql database connectivity

我的目标是比较oldwifi名称,oldwifipassword和inputbox1以及输入box2.I我想允许用户在确认旧的wifiname和密码后才能更改wifi名称和密码。

以下是我的代码。

<html>
<body>
<?php
$con = mysql_connect("localhost","user","password");
$oldwifi = mysql_real_escape_string($_POST['oldwifi']);
$oldpass = mysql_real_escape_string($_POST['oldpass']);
$sql="UPDATE test SET dboldwifi = '".$oldwifi."',dboldpass = '".$oldpass."'WHERE id = '1'";
mysql_select_db("test", $con);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
else if(mysql_query($sql)&&strcmp($oldwifi,'dboldwifi')==0&&strcmp($oldpass,'dboldpass')==0)
  {
        echo "New wifi and password saved successfully !";
  }
else
{
        echo "Old credentials mismatch";
        die('Error: ' . mysql_error());
}
mysql_close($con);
?>
</body>
</html>

我的数据库:

mysql> select * from test;
+-----------+-----------+----+
| dboldwifi | dboldpass | id |
+-----------+-----------+----+
| Tarzan    | Jane      |  1 |
+-----------+-----------+----+

HTML代码

<html>

<body>

<h1>A small example page to insert some data in to the MySQL database using PHP</h1>

<form action="insert.php" method="post">

Firstname: <input type="text" name="oldwifi" /><br><br>

Lastname: <input type="text" name="oldpass" /><br><br>



<input type="submit" />

</form>

</body>
</html>

输出结果:旧凭据mismatchError:

我是初学者在php mysql需要帮助的地方我做错了!

当我尝试插入数据时,

下面是工作代码;

<html>
<body>
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("test", $con);

$sql="INSERT INTO test (dboldwifi, dboldpass)VALUES ('$_POST[oldwifi]','$_POST[oldpass]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
mysql_close($con)
?>
</body>
</html>

谢谢!

1 个答案:

答案 0 :(得分:0)

此代码非常糟糕,您的操作顺序错误。尝试清理代码,这样可以更容易地进行调试。

elseif(mysql_query($sql)&&strcmp($oldwifi,'dboldwifi')==0&&strcmp($oldpass,'dboldpass')==0)

这是字面意思:

if (updateTheRow && noDifferenceWifi && noDifferentPwd)

您想要说的是:

if (noDifferenceWifi && noDifferentPwd) {
    updateTheRow

此外,您正在比较用户输入的文字字符串'dboldwifi'。

试试这个:

<?php
// Set up your connection here. You can also put this in a config file
// but the whole point is to get access to the $pdo variable. 
// See line 17
$host = '127.0.0.1';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);

// Don't need to escape because PDO is awesome
$oldwifi = $_POST['oldwifi'];
$oldpass = $_POST['oldpass'];

// Now use that $pdo variable in your application to query, update, delete etc.
$stmt = $pdo->query('SELECT 1 FROM test WHERE dboldwifi=? AND dboldpass=?');
$stmt->execute([$oldwifi, $oldpass]);
$match = $stmt->fetchColumn();

if ($match) {
    // Now update your row here using PDO. 
    // https://phpdelusions.net/pdo#dml
}