Once again this PHP/MySQL has bitten me.
I just wrote a PHP script that is used to delete 1 row in a MySQL database when the user submits a form. The script works great as is but now I want to extract 1 record 'photoname' from the row that is to be deleted. The record 'photoname' is to be used in the email that is to be sent to the webmaster(so I know which photograph to delete from the server). I spent all day and night yesterday and most of this morning trying to get it to work. Here is the script but I removed the part that I was working on the get the photoname record(so as not to confuse anyone)...
<?php
// DEFINE VARIABLES
$homeid = $_POST['homeid'];
$username = $_POST['username'];
$password = $_POST['password'];
$submit = $_POST['submit'];
$to = "[email protected]";
$message = "Username: $username has deleted their home.\n Please delete their photo id# $photoname.";
//CHECK IF SUBMIT WAS PRESSED
if (isset($submit)) {
$DBhost = "localhost";
$DBuser = "****";
$DBpass = "****";
$DBname = "homesubmit";
$table = "sellersinfo";
//DELETE DATA FROM DATABASE
mysql_connect($DBhost, $DBuser, $DBpass) or die ("Unable to connect to database. Please try again.");
mysql_select_db("$DBname") or die ("Unable to connect to database. Please try again.");
$query= "DELETE FROM $table WHERE homeid = '$homeid' AND password = '$password'";
mysql_query($query);
if (mysql_affected_rows() < 1) {
echo "Sorry, please try again.";
} else {
echo "Your home submission was successfully deleted.";
mail($to, "Webmaster, please delete: $photoname.", $message, "From: Triad Friendly Homes- Script Manager.");
}
}
mysql_close();
?>
Any ideas about what I should add to extract the photoname from the database? Thanks for having a look.Believe it or not I actually got the script to work. I actually found the code snippet on this forum that I needed. Notice that I have made two connections to the database. Can 1 connection be made where the code would still work but be more efficient? Here is my result...
<?php
// DEFINE VARIABLES
$homeid = $_POST['homeid'];
$username = $_POST['username'];
$password = $_POST['password'];
$submit = $_POST['submit'];
$to = "[email protected]";
//CHECK IF SUBMIT WAS PRESSED
if (isset($submit)) {
/*GET VARIABLE PHOTONAME FROM DATABASE(THIS IS NEW)*/
$DBhost = "localhost";
$DBuser = "triad";
$DBpass = "****";
$DBname = "****";
$table = "sellersinfo";
mysql_connect($DBhost, $DBuser, $DBpass) or die ("Unable to connect to database. Please try
again.");
mysql_select_db("$DBname") or die ("Unable to connect to database. Please try again.");
$aresult=mysql_query("SELECT photoname FROM $table WHERE homeid='$homeid' AND password='$password'");
if(mysql_num_rows($aresult)){
while($r=mysql_fetch_array($aresult)){
$photoname=$r["photoname"];
}
}
mysql_close();
//DELETE DATA FROM DATABASE
mysql_connect($DBhost, $DBuser, $DBpass) or die ("Unable to connect to database. Please try
again.");
mysql_select_db("$DBname") or die ("Unable to connect to database. Please try again.");
$query= "DELETE FROM $table WHERE homeid = '$homeid' AND password = '$password'";
mysql_query($query);
if (mysql_affected_rows() < 1) {
echo "Sorry, please try again.";
} else {
echo "Your home submission was successfully deleted.";
$message = "Username: $username has deleted their home.\n Please delete their photo id#
$photoname.";
mail($to, "Webmaster, please delete $photoname.", $message, "From: Triad Friendly Homes-
Script Manager.");
}
}
mysql_close();
?>
Thanks for having a look. you only need 1 connection. once you made that connection it is good for the whole script. so delete the last connect and select_db from the bottom.Thanks Scoutt.
I just wrote a PHP script that is used to delete 1 row in a MySQL database when the user submits a form. The script works great as is but now I want to extract 1 record 'photoname' from the row that is to be deleted. The record 'photoname' is to be used in the email that is to be sent to the webmaster(so I know which photograph to delete from the server). I spent all day and night yesterday and most of this morning trying to get it to work. Here is the script but I removed the part that I was working on the get the photoname record(so as not to confuse anyone)...
<?php
// DEFINE VARIABLES
$homeid = $_POST['homeid'];
$username = $_POST['username'];
$password = $_POST['password'];
$submit = $_POST['submit'];
$to = "[email protected]";
$message = "Username: $username has deleted their home.\n Please delete their photo id# $photoname.";
//CHECK IF SUBMIT WAS PRESSED
if (isset($submit)) {
$DBhost = "localhost";
$DBuser = "****";
$DBpass = "****";
$DBname = "homesubmit";
$table = "sellersinfo";
//DELETE DATA FROM DATABASE
mysql_connect($DBhost, $DBuser, $DBpass) or die ("Unable to connect to database. Please try again.");
mysql_select_db("$DBname") or die ("Unable to connect to database. Please try again.");
$query= "DELETE FROM $table WHERE homeid = '$homeid' AND password = '$password'";
mysql_query($query);
if (mysql_affected_rows() < 1) {
echo "Sorry, please try again.";
} else {
echo "Your home submission was successfully deleted.";
mail($to, "Webmaster, please delete: $photoname.", $message, "From: Triad Friendly Homes- Script Manager.");
}
}
mysql_close();
?>
Any ideas about what I should add to extract the photoname from the database? Thanks for having a look.Believe it or not I actually got the script to work. I actually found the code snippet on this forum that I needed. Notice that I have made two connections to the database. Can 1 connection be made where the code would still work but be more efficient? Here is my result...
<?php
// DEFINE VARIABLES
$homeid = $_POST['homeid'];
$username = $_POST['username'];
$password = $_POST['password'];
$submit = $_POST['submit'];
$to = "[email protected]";
//CHECK IF SUBMIT WAS PRESSED
if (isset($submit)) {
/*GET VARIABLE PHOTONAME FROM DATABASE(THIS IS NEW)*/
$DBhost = "localhost";
$DBuser = "triad";
$DBpass = "****";
$DBname = "****";
$table = "sellersinfo";
mysql_connect($DBhost, $DBuser, $DBpass) or die ("Unable to connect to database. Please try
again.");
mysql_select_db("$DBname") or die ("Unable to connect to database. Please try again.");
$aresult=mysql_query("SELECT photoname FROM $table WHERE homeid='$homeid' AND password='$password'");
if(mysql_num_rows($aresult)){
while($r=mysql_fetch_array($aresult)){
$photoname=$r["photoname"];
}
}
mysql_close();
//DELETE DATA FROM DATABASE
mysql_connect($DBhost, $DBuser, $DBpass) or die ("Unable to connect to database. Please try
again.");
mysql_select_db("$DBname") or die ("Unable to connect to database. Please try again.");
$query= "DELETE FROM $table WHERE homeid = '$homeid' AND password = '$password'";
mysql_query($query);
if (mysql_affected_rows() < 1) {
echo "Sorry, please try again.";
} else {
echo "Your home submission was successfully deleted.";
$message = "Username: $username has deleted their home.\n Please delete their photo id#
$photoname.";
mail($to, "Webmaster, please delete $photoname.", $message, "From: Triad Friendly Homes-
Script Manager.");
}
}
mysql_close();
?>
Thanks for having a look. you only need 1 connection. once you made that connection it is good for the whole script. so delete the last connect and select_db from the bottom.Thanks Scoutt.