[SOLVED] PHP Update problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Axllrod
Forum Newbie
Posts: 12
Joined: Tue Jun 12, 2012 4:15 pm
Location: Belgium

[SOLVED] PHP Update problem

Post by Axllrod »

Hi, i have been struggeling with a bit of code for the past few days. I'm trying to make an update form that only update the filled in fields. So it's supposed to ignore the empty fields. I wrote a bit of code that ignores the empty fields as following:

Code: Select all

<?php  
session_start(); 

include('connection.php');

$achternaam = $_SESSION['achternaam']; 
$voornaam = $_SESSION['voornaam']; 
$pwd = $_POST['admin']; 
$adres = $_POST['adres']; 
$postcode = $_POST['postcode'];
$gemeente = $_POST['gemeente']; 
$gsm = $_POST['gsm']; 
$telefoon = $_POST['telefoon']; 

//Sanatize input
$pwd = stripslashes($pwd);
$pwd = mysql_real_escape_string($pwd);
$adres = stripslashes($adres);
$adres = mysql_real_escape_string($adres);
$postcode = stripslashes($postcode);
$postcode = mysql_real_escape_string($postcode);
$gemeente = stripslashes($gemeente);
$gemeente = mysql_real_escape_string($gemeente);
$gsm = stripslashes($gsm);
$gsm = mysql_real_escape_string($gsm);
$telefoon = stripslashes($telefoon);
$telefoon = mysql_real_escape_string($telefoon);

$result = mysql_query("SELECT * FROM ledenlijst WHERE Voornaam='$voornaam' AND Familienaam='$achternaam' AND pwd='$pwd'"); 
if(!$result)  {  
	echo "U heeft een verkeerd paswoord ingegeven." ?><br> <a href="../Accbeheer.php">Ga terug</a> <?php ;  
	}   
	
	else 
	{  
	if(trim($adres) != ''){$sql=mysql_query("UPDATE ledenlijst SET Adres='$adres' WHERE Familienaam='$achternaam' AND Voornaam='$voornaam' AND pwd='$pwd'");}else{echo mysql_error();}
	if(trim($postcode) != ''){$sql=mysql_query("UPDATE ledenlijst SET Postcode='$postcode' WHERE Familienaam='$achternaam' AND Voornaam='$voornaam' AND pwd='$pwd'");} else{echo mysql_error();}
	if(trim($gemeente) != ''){$sql=mysql_query("UPDATE ledenlijst SET Gemeeente='$gemeente' WHERE Familienaam='$achternaam' AND Voornaam='$voornaam' AND pwd='$pwd'");} else{echo mysql_error();}
	if(trim($gsm) != ''){$sql=mysql_query("UPDATE ledenlijst SET GSM='$gsm' WHERE Familienaam='$achternaam' AND Voornaam='$voornaam' AND pwd='$pwd'");} else{echo mysql_error();}
	if(trim($telefoon) != ''){$sql=mysql_query("UPDATE ledenlijst SET Telefoon='$telefoon' WHERE Familienaam='$achternaam' AND Voornaam='$voornaam' AND pwd='$pwd'");} else{echo mysql_error();}

	echo "The edit was successful" ?><br> <a href="../AccBeheer.php">Go back</a> <?php ;
	}   

?>
The strange thing is that it doesn't give any errors even though there is no update in the database. There is a connection with the database, so that can't be the problem.
Thanks in advance.
Last edited by Axllrod on Thu Apr 25, 2013 2:55 pm, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP Update problem

Post by social_experiment »

Code: Select all

<?php
 if(trim($adres) != ''){$sql=mysql_query("UPDATE ledenlijst SET Adres='$adres' WHERE Familienaam='$achternaam' AND Voornaam='$voornaam' AND pwd='$pwd'");}else{echo mysql_error();}
?>
This line of code will only give an error if is $adres is empty; and since no query has been executed, you would probably still receive no error (unless a previous mysql error occured).

You should modify the code to look like this

Code: Select all

<?php
 if ($trim($adres) != '') {
      $sql = mysql_query("UPDATE ledenlijst SET Adres = '$adres' WHERE Familienaam = '$acternaam' AND Voornaam = '$voornaam' AND pwd = '$pwd'") or die(mysql_error());
 }
?>
If $adres has a value the query will execute and should an error occur the script will stop and tell you the problem.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Axllrod
Forum Newbie
Posts: 12
Joined: Tue Jun 12, 2012 4:15 pm
Location: Belgium

Re: PHP Update problem

Post by Axllrod »

That is a good idea indeed. I'll see what the new code gives me.
Thanks for the quick reply!

EDIT: Made it work with the error, thanks for the help!
Code:

Code: Select all

<?php  
session_start(); 

include('connection.php');

$achternaam = $_SESSION['achternaam']; 
$voornaam = $_SESSION['voornaam']; 
$pwd = $_POST['admin']; 
$adres = $_POST['adres']; 
$postcode = $_POST['postcode'];
$gemeente = $_POST['gemeente']; 
$gsm = $_POST['gsm']; 
$telefoon = $_POST['telefoon']; 

//Sanatize input
$pwd = stripslashes($pwd);
$pwd = mysql_real_escape_string($pwd);
$adres = stripslashes($adres);
$adres = mysql_real_escape_string($adres);
$postcode = stripslashes($postcode);
$postcode = mysql_real_escape_string($postcode);
$gemeente = stripslashes($gemeente);
$gemeente = mysql_real_escape_string($gemeente);
$gsm = stripslashes($gsm);
$gsm = mysql_real_escape_string($gsm);
$telefoon = stripslashes($telefoon);
$telefoon = mysql_real_escape_string($telefoon);

$sql="SELECT * FROM ledenlijst WHERE Voornaam='$voornaam' AND Familienaam='$achternaam' AND pwd='$pwd'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if($count !== 1)  {  
echo "U heeft een verkeerd paswoord ingegeven." ?><br> <a href="../AccBeheer.php">Ga terug</a> <?php ;  
}   

else 
{  
if(!empty($adres)){mysql_query("UPDATE ledenlijst SET Adres = '$adres' WHERE Familienaam = '$achternaam' AND Voornaam = '$voornaam' AND pwd = '$pwd'") or die(mysql_error());}
if(!empty($postcode)){mysql_query("UPDATE ledenlijst SET Postcode = '$postcode' WHERE Familienaam = '$achternaam' AND Voornaam = '$voornaam' AND pwd = '$pwd'") or die(mysql_error());}
if(!empty($gemeente)){mysql_query("UPDATE ledenlijst SET Gemeente = '$gemeente' WHERE Familienaam = '$achternaam' AND Voornaam = '$voornaam' AND pwd = '$pwd'") or die(mysql_error());}
if(!empty($gsm)){mysql_query("UPDATE ledenlijst SET GSM = '$gsm' WHERE Familienaam = '$achternaam' AND Voornaam = '$voornaam' AND pwd = '$pwd'") or die(mysql_error());}
if(!empty($telefoon)){mysql_query("UPDATE ledenlijst SET Telefoon = '$telefoon' WHERE Familienaam = '$achternaam' AND Voornaam = '$voornaam' AND pwd = '$pwd'") or die(mysql_error());}

echo "Uw gegevens zijn succesvol veranderd." ?><br> <a href="../AccBeheer.php">Ga terug</a> <?php ;
}   

?>
Post Reply