我的更新脚本无法运行..我不知道我失踪了什么.. 但我不能更新表...去w3school了解php中的更新但仍然无法工作... -noob coder -
<?php
include 'Core/init.php';
protect_page();
include 'Includes/Overall/overallheader.php';
?>
<h1>Update School Year and Semester</h1>
<?php
$con=mysqli_connect("localhost","root","1234","database3");
// Check connection
$sy = $_POST['school_year'];
$sem = $_POST['semester'];
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if ( isset($_POST['submit'] ) )
{
$sql = "UPDATE `activesys` SET `activeschoolyear` = '$sy' AND `activesemester` = '$sem'";
$exec = mysql_query($sql) or die(mysql_error());
}
?>
<form action="" method="post">
<ul>
<li>
School Year:<br />
<input type="text" name="school_year">
</li>
<li>
Semester:<br />
<input type="text" name="semester">
</li>
<li>
<input type="submit" value="submit">
</li>
</ul>
</form>
<?php
include 'Includes/Overall/overallfooter.php';
?>
答案 0 :(得分:1)
即使它没有直接相关:
请确保使用mysql_escape_string转义从$ _POST或$ _GET加载的请求数据。否则,在查询中注入SQL会很容易,这样就可以在系统中运行有害的sql,比如删除数据库或操作数据。
答案 1 :(得分:0)
你错过了更新查询中的条件..试试这个
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
$sql = "UPDATE `activesys` SET `activeschoolyear` = '$sy' AND `activesemester` = '$sem'" WHERE some_column=some_value;
答案 2 :(得分:0)
首先,您混合mysqli
和mysql
。其次,mysql_query
/ mysqli_query
有两个参数。第三,你的sql不对。
因此,请更改您的$exec = mysql_query($sql) or die(mysql_error())
到$exec = mysqli_query($con,$sql) or die(mysqli_error($con));
将您的sql更改为:
$sql = "UPDATE `activesys` SET `activeschoolyear` = '$sy' , `activesemester` = '$sem'";
但是你在这里没有where条件,如果在你的表中你设置了一个 anto-increment键并将其设置为主键,你可以添加一些条件结束你的sql,否则它会 更新所有记录。
$sql = "UPDATE `activesys` SET `activeschoolyear` = '$sy' , `activesemester` = '$sem' where id={$id}"
$ id是您表中的一个记录。
这应该适用于您执行更新..
答案 3 :(得分:0)
尝试使用:
1)使用标签
的“action”属性<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
2)检查您的表格是否已提交:
if(isset($_POST['submit']))
3)在sql查询中使用WHERE条件。您的最终查询应如下所示:
<?php
if(isset($_POST['submit']))
{
$con = mysqli_connect("localhost","root","1234","database3");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$query = "
UPDATE `activesys`
SET `activeschoolyear` = '$sy',`activesemester` = '$sem'
WHERE `columnName` = `columnValue`
";
mysqli_query($con,$query);
}