gpt4 book ai didi

php - 无法编辑数据库中的数据

转载 作者:行者123 更新时间:2023-11-30 21:46:26 25 4
gpt4 key购买 nike

我创建此页面是为了编辑数据库中的数据,但在我单击提交按钮填写所有表单后,它一直弹出验证错误,但我已经填写了所有表单。请帮助我。

include('conn.php');

// creates the edit record form

// since this form is used multiple times in this file, I have made it a function that is easily reusable

function renderForm($noid, $listVendor, $department, $district, $report, $error)

{


// if there are any errors, display them

if ($error != '')

{

echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';

}


}



// check if the form has been submitted. If it has, process the form and save it to the database

if (isset($_POST['submit']))

{

// confirm that the 'id' value is a valid integer before getting the form data

if (is_numeric($_POST['noid']))

{

// get form data, making sure it is valid

$noid = $_POST['noid'];

$listVendor = mysql_real_escape_string(htmlspecialchars($_POST['listVendor']));

$department = mysql_real_escape_string(htmlspecialchars($_POST['department']));

$district = mysql_real_escape_string(htmlspecialchars($_POST['district']));

$report = mysql_real_escape_string(htmlspecialchars($_POST['report']));




// check that all fields are filled in

if ($listVendor == '')

{

// generate error message

$error = 'ERROR: Please fill in all required fields!';



//error, display form

renderForm($noid, $listVendor, $department, $district, $report, $error);

}

else

{

// save the data to the database

mysql_query("UPDATE tblvendorreport SET listVendor='$listVendor', department='$department', district='$district', report='$report' WHERE noid='$noid'")

or die(mysql_error());



// once saved, redirect back to the view page

header("Location: bdl-VendorReport.php");

}

}

else

{

// if the 'id' isn't valid, display an error

echo 'Error!';

}

}

else

// if the form hasn't been submitted, get the data from the db and display the form

{



// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)

if (isset($_GET['noid']) && is_numeric($_GET['noid']) && $_GET['noid'] > 0)

{

// query db

$noid = $_GET['noid'];

$result = mysql_query("SELECT * FROM tblvendorreport WHERE noid=$noid")

or die(mysql_error());

$row = mysql_fetch_array($result);



// check that the 'id' matches up with a row in the database

if($row)

{



// get data from db

$listVendor = $row['listVendor'];

$district = $row['district'];

$report = $row['report'];




// show form

renderForm($noid, $listVendor, $department, $district, $report,'');


}

else

// if no match, display result

{

echo "No results!";

}

}

else

// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error

{

echo 'Error!';

}

}

最佳答案

因为你写的是你原来的赌注,否则显示这个错误

<?php
include('conn.php');
function renderForm($noid, $listVendor, $department, $district, $report, $error){
if ($error != ''){
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}else{
echo '<div style="padding:4px; border:1px solid red; color:green;">'.$noid.'<br>'.$listVendor.'<br>'.$department.'<br>'.$district.'<br>'.$report.'</div>';
}
}

if (isset($_POST['submit'])){ // if method post
if (is_numeric($_POST['noid'])){ // if is numeric
$noid = $_POST['noid'];
$listVendor = mysql_real_escape_string(htmlspecialchars($_POST['listVendor']));
$department = mysql_real_escape_string(htmlspecialchars($_POST['department']));
$district = mysql_real_escape_string(htmlspecialchars($_POST['district']));
$report = mysql_real_escape_string(htmlspecialchars($_POST['report']));

if ($listVendor == ''){
$error = 'ERROR: Please fill in all required fields!';
renderForm($error);
}else{
mysql_query("UPDATE tblvendorreport SET listVendor='$listVendor', department='$department', district='$district', report='$report' WHERE noid='$noid'")
or die(mysql_error());
header("Location: bdl-VendorReport.php");
}
}else{ // if is not numeric
$error = 'Error!';
renderForm($error);
}
}else if ((isset($_GET['noid'])) AND (is_numeric($_GET['noid'])) AND ($_GET['noid']>0)){ // if method get
$noid = $_GET['noid'];
$result = mysql_query("SELECT * FROM tblvendorreport WHERE noid=$noid")
or die(mysql_error());
$row = mysql_fetch_array($result);
if(@$row){
$listVendor = $row['listVendor'];
$district = $row['district'];
$report = $row['report'];
$department = $row['department'];
$error = '';
renderForm($noid,$listVendor,$department,$district,$report,$error);
}else{
$error = 'No results!';
renderForm($error);
}
} // else if
?>

关于php - 无法编辑数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49247537/

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