This answer is based on your original post and not marking it as an edit, should anyone wonder.
The reason why your upload()
function is failing, is because you haven't included the mysql_query()
function, along with a few missing parts. (Parts, being quotes/brackets).
function upload(){
mysql_query("UPDATE frogs SET name = '$name' WHERE name = 'TreeFrog'");
}
A word of advice though:
Your present code is open to SQL injection.
Use mysqli
with prepared statements, or PDO with prepared statements, they're much safer.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
also or die(mysql_error())
to mysql_query()
.
Edit:
To fire up your function using a PHP method, I recommend you change the
<input type="submit" onclick="update()">
to
<input type="submit" name="update_db" value="Update">
and wrapping an isset()
around it.
I.e.:
<?php
function upload(){
mysql_query("UPDATE frogs SET name = '$name' WHERE name = 'TreeFrog'");
}
if(isset($_POST['update_db'])){
echo upload();
}
?>
However, you will need <form></form>
tags around your form's element(s) and a post method.
<form action = "" method = "post">
<input type="text" id="name" value='<?=$name?>'>
<input type="submit" name="update_db" value="Update">
</form>
Edit #2:
This is a mysqli_
method, please change the DB credentials to match yours if they do not match.
I had to remove the upload()
function, it was giving me too much trouble.
A hidden input has been added in the form, which is essential to doing updates like these.
<?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>
You can also redirect to the same page by adding this at the top:
<?php
ob_start();
?>
then adding this after your query:
if($results){
header("Location: http://www.yoursite.com/update_frogs.php");
}