<?php
//connect to server
$connect = mysql_connect("localhost","name","password");
//connect to db
mysql_select_db("complexm_pondlife", $connect);
//query the db
$query = mysql_query("SELECT * FROM frogs");
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<button onclick="show()">SHOW DATA</button>
<p id="clip"style="visibility: hidden">
<?php
WHILE($rows = mysql_fetch_array($query)):
$name = $rows['name'];
$age = $rows['age'];
$sound = $rows['sound'];
$id = $rows['id'];
?>
<?php
echo $id.") "."Name: ";
?>
<form action = "" method = "post">
<input type="text" id="name" value='<?=$name?>'>
<input type="submit" name="update_db" value="Update">
</form>
<?php
echo "Age: "."$age<br><br>";
echo "Sound: "."$sound<br><br>";
echo "___________<br><br>";
endwhile;
?>
</p>
<?php
function upload(){
mysql_query("UPDATE frogs SET name = '$name' WHERE name = '$name'");
}
if(isset($_POST['update_db'])){
echo upload();
}
?>
<script>
function show(){
document.getElementById('clip').style.visibility="visible";
}
</script>
此代码告诉我:注意:未定义的变量:第70行的/home1/complexm/public_html/projects.php中的名称
我不知道为什么。所以,如果有人能告诉我,我想知道。如果语法错误请告诉我!
答案 0 :(得分:3)
这个答案是基于你的original post而不是标记为编辑,如果有人想知道的话。
你的upload()
功能失败的原因是因为你没有包含mysql_query()
功能,还有一些缺失的部分。 (部件,引号/括号)。
function upload(){
mysql_query("UPDATE frogs SET name = '$name' WHERE name = 'TreeFrog'");
}
尽管如此:
您现有的代码向SQL injection开放
使用mysqli
with prepared statements或PDO with prepared statements,它们更安全。
将error reporting添加到文件的顶部,这有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告应仅在暂存时完成,而不是生产。
or die(mysql_error())
到mysql_query()
。
修改强>:
要使用PHP方法启动您的功能,我建议您更改
<input type="submit" onclick="update()">
到
<input type="submit" name="update_db" value="Update">
并在其周围包裹isset()
。
即:
<?php
function upload(){
mysql_query("UPDATE frogs SET name = '$name' WHERE name = 'TreeFrog'");
}
if(isset($_POST['update_db'])){
echo upload();
}
?>
但是,您需要在表单的元素和帖子方法周围添加<form></form>
个标记。
<form action = "" method = "post">
<input type="text" id="name" value='<?=$name?>'>
<input type="submit" name="update_db" value="Update">
</form>
编辑#2 :
这是mysqli_
方法,如果它们不匹配,请更改数据库凭据以匹配您的凭据。
我不得不删除upload()
功能,这给我带来了太多麻烦。
表单中添加了隐藏的输入,这对于执行此类更新至关重要。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//connect to server
$DB_HOST = 'localhost';
$DB_USER = 'name';
$DB_PASS = 'password';
$DB_NAME = 'complexm_pondlife';
$link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($link->connect_errno > 0) {
die('Connection failed [' . $link->connect_error . ']');
}
//query the db
$query = mysqli_query($link,"SELECT * FROM frogs");
?>
<button onclick="show()">SHOW DATA</button>
<p id="clip"style="visibility: hidden">
<?php
WHILE($rows = mysqli_fetch_array($query)):
$name = $rows['name'];
$age = $rows['age'];
$sound = $rows['sound'];
$id = $rows['id'];
?>
<?php
echo $id.") "."Name: ";
?>
<form action = "" method = "post">
<input type="text" id="name" name="thename" value="<?php echo $name; ?>">
<input type="hidden" name="the_id" value="<?php echo $id; ?>">
<input type="submit" name="update_db" value="Update">
<br>
</form>
<?php
echo "Age: "."$age<br><br>";
echo "Sound: "."$sound<br><br>";
echo "___________<br><br>";
endwhile;
?>
</p>
<?php
if(isset($_POST['update_db'])){
$theid = stripslashes($_POST['the_id']);
$theid = mysqli_real_escape_string($link,$_POST['the_id']);
$thename = stripslashes($_POST['thename']);
$thename = mysqli_real_escape_string($link,$_POST['thename']);
$results= mysqli_query($link, "UPDATE frogs SET name = '$thename' WHERE id = '$theid'");
}
?>
<script>
function show(){
document.getElementById('clip').style.visibility="visible";
}
</script>
您还可以通过在顶部添加以下内容重定向到同一页面:
<?php
ob_start();
?>
然后在查询后添加:
if($results){
header("Location: http://www.yoursite.com/update_frogs.php");
}