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!
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:
<?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.
<?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).
<?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