简体   繁体   English

PHP MySQL 语句不更新数据库

[英]PHP MySQL Statements not Updating Database

After much editing and checking tutorial sites.经过多次编辑和检查教程网站。 Code currently not calling info from Database and when clicking Approve button, does not edit database.代码当前未从数据库调用信息,并且在单击批准按钮时不编辑数据库。 I do have a column identifier named Reg_ID which can specify which column of data you choose to edit.我确实有一个名为 Reg_ID 的列标识符,它可以指定您选择编辑的数据列。 The form is submitting, just clears the information that I enter in and doesn't store the data.表单正在提交,只是清除我输入的信息,不存储数据。

This file is named Approve Deny Prayer Request.此文件名为“批准拒绝祷告请求”。

<?php
$DB_HOST = "XXXXXXX";
$DB_NAME = "XXXXXXX";
$DB_PASS = "XXXXXXX";
$DB_USER = "XXXXXXX";

$link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($link->connect_errno > 0) {
die('Connection failed [' . $db->connect_error . ']');
}

$query = "SELECT * FROM Request";
$result = mysqli_query($link,$query); //<----- Added link
$row = mysqli_fetch_array($result);

if(isset($_POST['add'])){

$id = mysqli_real_escape_string($link,$_POST['id']);
$firstname = mysqli_real_escape_string($link,$_POST['first']);
$lastname = mysqli_real_escape_string($link,$_POST['last']);
$phone = mysqli_real_escape_string($link,$_POST['phone']);

$query2=mysqli_query($link,"UPDATE Request SET Reg_F_Name='$firstname',     Reg_L_Name='$lastname',Reg_Request='$phone' WHERE id='$id'" );

if($query2){
header("Location: fbcaltusprayerorg.ipagemysql.com");
}

} // brace if(isset($_POST['add']))

?>

<form action="" method="post">

<table>
<input type="hidden" name="id" value="<? echo "$row[Reg_ID]" ?>">

<tr>
<td>First Name:</td>
<td><input type="text" name="first" value="<? echo "$row[Reg_F_Name]" ?>"></td>
</tr>

<tr>
<td>Last Name:</td>
<td><input type="text" name="last" value="<? echo "$row[Reg_L_Name]" ?>"></td>
</tr>

<tr>
<td>Prayer Request:</td>
<td><input type="text" name="phone" value="<? echo "$row[Reg_Request]" ?>"></td>
</tr>

</table>
<input name="add" type="submit" id="add" value="Approve Prayer Request">

</form>

Firstly, your initial code did not contain an opening <form> tag;首先,您的初始代码不包含开始的<form>标签; that has been included below.这已包括在下面。

The way you're attempting to run your code is leaving you open to SQL injection .您尝试运行代码的方式让您对SQL 注入持开放态度。

Now, here's what you need to do.现在,这就是你需要做的。

  • Create a column named id and set it to AUTO_INCREMENT if needed, but not required;创建一个名为id的列,并在需要时将其设置为AUTO_INCREMENT ,但不是必需的; just as long as there is some data related to it and holds a unique name/id.只要有一些与之相关的数据并拥有唯一的名称/ID。
  • Create a hidden field called/named id创建一个名为/命名为id的隐藏字段

Then use UPDATE along with SET and a WHERE clause.然后将 UPDATE 与 SET 和 WHERE 子句一起使用。

Sidenote: This will automatically redirect you to the page's filename you've called it.旁注:这会自动将您重定向到您调用的页面文件名。

In this example, I used header("Location: http://www.example.com/update.php");在这个例子中,我使用了header("Location: http://www.example.com/update.php");

Replace the DB credentials with your own.将数据库凭据替换为您自己的凭据。

<?php
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";

$link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($link->connect_errno > 0) {
  die('Connection failed [' . $db->connect_error . ']');
}

$query = "SELECT * FROM Request";
$result = mysqli_query($link,$query); //<----- Added link
$row = mysqli_fetch_array($result);

if(isset($_POST['add'])){

$id = mysqli_real_escape_string($link,$_POST['id']);
$firstname = mysqli_real_escape_string($link,$_POST['first']);
$lastname = mysqli_real_escape_string($link,$_POST['last']);
$phone = mysqli_real_escape_string($link,$_POST['phone']);

$query2=mysqli_query($link,"UPDATE Request SET Reg_F_Name='$firstname', Reg_L_Name='$lastname',Reg_Request='$phone' WHERE id='$id'" );

if($query2){
header("Location: http://www.example.com/update.php");
}

} // brace if(isset($_POST['add']))

?>

<form action="" method="post">

<table>
<input type="hidden" name="id" value="<? echo "$row[id]" ?>">

<tr>
<td>First Name:</td>
<td><input type="text" name="first" value="<? echo "$row[Reg_F_Name]" ?>"></td>
</tr>

<tr>
<td>Last Name:</td>
<td><input type="text" name="last" value="<? echo "$row[Reg_L_Name]" ?>"></td>
</tr>

<tr>
<td>Prayer Request</td>
<td><input type="text" name="phone" value="<? echo "$row[Reg_Request]" ?>"></td>
</tr>

</table>
<input name="add" type="submit" id="add" value="Approve Prayer Request">

</form>

where is the call to update the database with your sql statement?用你的 sql 语句更新数据库的调用在哪里?

I have a function that normally I just for update of the database.我有一个通常只用于更新数据库的功能。 I also make sure to add column for each table like UpdateDtTm and add that to the end of my update.我还确保为每个表添加列,如 UpdateDtTm,并将其添加到我的更新末尾。 That way you know you are going to always update something on an update statement.这样您就知道您将始终在更新语句中更新某些内容。 Also make sure to use a key and a unique id to make sure you only update the row you want.还要确保使用一个键和一个唯一的 id 来确保你只更新你想要的行。

Also, try using this syntax另外,尝试使用此语法

$query2 = "Update Request set Reg_F_Name = $row[Reg_F_Name], Reg_L_Name = $row['Reg_L_Name],    Reg_Request = $row['Reg_Request'], UpdateDtTM = Now() where <A UNIQUE KEY ROW> = <UNIQUE ID>. 

 $result = db_update ("updating request in some location", $sql,"update");


 function db_update($function_name,$sql,$type) {

    // Get access to PHP global variables
    global $database;
    //if the database value is not pulled from the global array make sure
    //the system has it based on the Session value set on load
    if (! $database) {
        $database = $_SESSION['database'];
    }

    // Now authenticate the user with the database
    $db = db_connect($database);
    // Run SQL Query
mysql_query($sql);
// Mysql won't return a $result for UPDATE, so have to test with mysql_affected_rows
// mysql also won't do an update if the values are the same, so you could
// possibly have an instance where nothing is change and this fails
// got around this by adding an updated column that is increased by 1 everytime
// an update is performed.  this ensures that you always have something updated
if ( mysql_affected_rows()==0 ) {

    // Unable to update
    $error = "db_update error<br>$sql<br>".mysql_errno()." - ".mysql_error();
    database_error($error,$sql);

    // Exit the function after error
    exit;

}

// Do nothing for this guy
// We don't need to return anything
return;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM