无法编辑数据库

时间:2018-03-13 02:49:43

标签: php html mysql forms

我创建此页面来编辑数据库中的数据,但是在我单击提交按钮填充所有表单后,它仍然保持测试错误,但我已经填写了所有表单。请帮帮我。

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!';

}

}

1 个答案:

答案 0 :(得分:0)

因为您为原始投注而写,否则显示此错误

<?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
?>