gpt4 book ai didi

php - 如何使用下拉菜单、文本字段和提交按钮将我的投票更新到我的 sql 中?

转载 作者:行者123 更新时间:2023-11-29 02:57:46 25 4
gpt4 key购买 nike

如何使用下拉菜单、文本字段和提交按钮将我的投票更新到我的 sql 中?我非常需要帮助来修复我的代码。快 1 周了,我还是想不通。如果我在文本字段中输入 20,就会出现这种情况,它将在 mysql 中的 totalvotes 列中添加 +20

这是我的文本框和提交按钮

<?php
$teacherz = $_POST['teacher_dropdown'];
$votecount = $_POST['votecount'];
$sql = "SELECT facultyname FROM subj_eva";

echo "<form method='post' action='cof_test.php'>
<input type='text' name='votecount'>
<input type='submit' name='submit' value='Submit now'>
</form>";


$teacherz = $_POST['teacher_dropdown'];
$votecount = $_POST['votecount'];
$sql = "SELECT facultyname FROM subj_eva";

if (isset($_POST['submit'])) {
$sql = "UPDATE $subj_eva SET facultyname='$teacherz', totalvotes=totalvotes + '$votecount'";
}
$result=mysql_query($sql);
?>

这是我的下拉菜单

<?php
mysql_connect('localhost', 'root', 'password');
mysql_select_db('ramon_pascual');

$sql = "SELECT facultyname FROM subj_eva";
$result = mysql_query($sql);

echo "<select name='teacher_dropdown'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value'" . $row['facultyname'] ."'>" . $row['facultyname'] . "</option>";
}
echo "</select>";

?>

最佳答案

我刚刚重构了你的代码

表单文件(index.php)

<?php
if (!$link = mysql_connect('localhost', 'root', 'password')) {
echo 'Could not connect to mysql';
exit;
}

if (!mysql_select_db('stackoverflow', $link)) {
echo 'Could not select database';
exit;
}

$sql = 'SELECT facultyname FROM subj_eva';
$result = mysql_query($sql, $link);

if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
?>


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
</head>
<body>
<form method='post' action='cof_test.php'>
<input type='text' name='votecount'>
<select name='teacher_dropdown'>
<?php
while ($row = mysql_fetch_assoc($result)) {
echo "<option value'" . $row['facultyname'] ."'>" . $row['facultyname'] . "</option>";
}
?>
</select>
<input type='submit' name='submit' value='Submit now'>
</form>

</body>
</html>
<?php mysql_free_result($result);?>

cof_test.php 文件

<?php
$teacherz = isset($_POST['teacher_dropdown']) ? $_POST['teacher_dropdown'] : "";
$votecount = isset($_POST['votecount']) ? $_POST['teacher_dropdown'] : "";

if (isset($_POST['submit'])) {
$sql = "UPDATE $subj_eva SET facultyname='$teacherz', totalvotes = totalvotes + '$votecount'";
}
$result=mysql_query($sql);

关于php - 如何使用下拉菜单、文本字段和提交按钮将我的投票更新到我的 sql 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28655854/

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