0

I am trying to check if a field is already filled with data in the database. If is is I want to update the information but if it's empty I want to insert the information into the database. But it doesn't work now :

$info = $_POST['info']; $gebruiker = $_POST['gebruiker']; $getnamequery = mysql_query("SELECT fysionummer FROM algemene_info WHERE fysionummer = '$gebruiker'"); $result = mysql_query($getnamequery) or die(mysql_error()); $row = mysql_fetch_row($result); if ($gebruiker === $row) { $sql = 'UPDATE algemene_info SET omschrijving=$info WHERE fysionummer=$gebruiker'; echo "Het update van de algemene informatie is geslaagd!"; } else { mysql_query("INSERT INTO algemene_info (`info_id`, `omschrijving`, `fysionummer`) VALUES (NULL, '$info', '$gebruiker')") or die (mysql_error()); echo "Het toevoegen van de oefening is geslaagd!"; } 
2
  • 2
    See INSERT... UPDATE ON DUPLICATE KEY Commented May 27, 2014 at 11:51
  • Please be aware that the mysql extension has been deprecated for quite some time now in favor of the mysqli and PDO extensions. It's use is highly discouraged. See stackoverflow.com/questions/12859942/… Commented May 27, 2014 at 13:00

3 Answers 3

0

Try this:

$info = $_POST['info']; $gebruiker = $_POST['gebruiker']; $getnamequery = mysql_query("SELECT fysionummer FROM algemene_info WHERE fysionummer = '$gebruiker'"); $result = mysql_query($getnamequery) or die(mysql_error()); $row = mysql_fetch_array($result); if ($gebruiker == $row['fysionummer']) { $sql = 'UPDATE algemene_info SET omschrijving=$info WHERE fysionummer=$gebruiker'; echo "Het update van de algemene informatie is geslaagd!"; } else { mysql_query("INSERT INTO algemene_info (`info_id`, `omschrijving`, `fysionummer`) VALUES (NULL, '$info', '$gebruiker')") or die (mysql_error()); echo "Het toevoegen van de oefening is geslaagd!"; } 
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

$info = $_POST['info']; $gebruiker = $_POST['gebruiker']; $result = mysqli_query("SELECT * FROM algemene_info WHERE fysionummer = '$gebruiker'"); $row = mysql_fetch_array($result); $qry = "INSERT INTO algemene_info (`info_id`, `omschrijving`, `fysionummer`) VALUES ({{$row['info_id']}},'$info', '$gebruiker') ON DUPLICATE KEY UPDATE omschrijving='$info'"; mysqli_query($qry); 

Comments

0
HI Complete solution as follows, $info = $_POST['info']; $gebruiker = $_POST['gebruiker']; $getnamequery = mysql_query("SELECT fysionummer FROM algemene_info WHERE fysionummer = '$gebruiker'"); $result = mysql_query($getnamequery) or die(mysql_error()); $num = mysql_num_rows($result); if($num != "") { $sql = 'UPDATE algemene_info SET omschrijving=$info WHERE fysionummer=$gebruiker'; mysql_query($sql) or die("!query"); echo "Het update van de algemene informatie is geslaagd!"; } else { mysql_query("INSERT INTO algemene_info (`info_id`, `omschrijving`, `fysionummer`) VALUES (NULL, '$info', '$gebruiker')") or die (mysql_error()); echo "Het toevoegen van de oefening is geslaagd!"; } 

1 Comment

Could you add some explanation along with your code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.