For an assessment of mine, I’m required to make it so an admin could login to the database and then either add a row to the database, edit an existing entry or delete a row if they need to. My edit and delete codes don’t record the changes made. I can add/remove information but the database doesn’t save it.
In my PHP files, Host, Username, Password, DB_Name and TBL_Name have content, but I haven’t added it for this.
This is my update.php.
[php]<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select database.
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);
// get value of id that sent from address bar
$id=$_GET[‘id’];
// Retrieve data from database
$sql=“SELECT * FROM $tbl_name WHERE id=’$id’”;
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
|
This is the page it goes to.
[php]<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select database.
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);
// update data in mysql database
$mysql_query= "UPDATE $tbl_name
SET
Plant_Family = ‘$plantf’,
Species = ‘$species’,
Genus = ‘$genus’,
Plant_Name = ‘$pname’,
Common_Name = ‘$cname’,
Synonymous_Name = ‘$sname’,
Type = ‘$type’,
Type_2 = ‘$type_2’,
Height = ‘$height’,
Propogation = ‘$propogation’,
Image = ‘$image’,
Reference = ‘$reference’
WHERE ‘ID’ = ‘$id’"
or die(mysql_error());
$result=mysql_query($mysql_query);
// if successfully updated.
if($result){
echo “Successful”;
echo “
”;
header( “refresh:1;admindatabase.php” );
}
else {
echo “Problem with query $sql, error was:” . mysql_error();
}
mysql_close();
?>[/php]
This is my delete function.
[php]<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select database.
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);
if (isset($_GET[‘id’]) && is_numeric($_GET[‘id’]))
{
$id = $_GET[‘id’];
$result = mysql_query(“DELETE FROM plant WHERE id=$id”)
or die(mysql_error());
header(“Location: admindatabase.php”);
}
else
{
header(“Location: admindatabase.php”);
}
?>[/php]
Is there something in the coding itself that I’m missing or that needs correcting? Outside of “You should use mysqli”, that is, I’m aware I should, I just don’t have the time to have it added and I plan to in the future at all times. I really hope someone can help me, my course teacher hasn’t been able to help me.