farahZ Posted May 3, 2013 Share Posted May 3, 2013 helloi am trying to submit data in a table in phpmyadmin the database is called "inshapewebsite" the table is "caloriescounter" the connection works fine for inserting news rows but what i'm trying to do now is to update the table if a row with the same ID and Date exsistsi.e: primary keys are ID and Date here's the code am working on // Create connection $con=mysqli_connect("localhost","root","","inshapewebsite"); // Check connection if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $clid=111; $date=date("m.d.y"); foreach ($_SESSION['foodTypes'] as $food) { $foodS= $foodS . ",". $food; } $result = mysql_query("Update caloriescounter set Lunch='$foodS' where ID='$clid' and Date='$date'"); if (mysql_affected_rows()==0) { $result = mysql_query("insert into caloriescounter (ID, Date, Lunch) VALUES ('$clid', '$date','$foodS')"); } ; mysqli_close($con); // session exists and has content // process the array list here. // after the processing, empty the session $_SESSION['foodTypes'] = array(); Link to comment https://forums.phpfreaks.com/topic/277572-check-if-rows-exist-in-mysql-update-if-no-create-new-row/ Share on other sites More sharing options...
Barand Posted May 3, 2013 Share Posted May 3, 2013 A single query will suffice INSERT INTO caloriescounter (ID, Date, Lunch) VALUES ('$clid', '$date','$foodS') ON DUPLICATE KEY UPDATE Lunch = '$foodS' Link to comment https://forums.phpfreaks.com/topic/277572-check-if-rows-exist-in-mysql-update-if-no-create-new-row/#findComment-1427921 Share on other sites More sharing options...
farahZ Posted May 3, 2013 Author Share Posted May 3, 2013 thanks am getting this error: Warning: mysql_query() expects parameter 1 to be string, Link to comment https://forums.phpfreaks.com/topic/277572-check-if-rows-exist-in-mysql-update-if-no-create-new-row/#findComment-1427927 Share on other sites More sharing options...
farahZ Posted May 3, 2013 Author Share Posted May 3, 2013 thats the code <?php session_start(); if (isset($_POST['add'])) { // check if an option has been selected if (empty($_POST['foodType'])) { echo 'You need to select some food!'; } else { if (!isset($_SESSION['foodTypes'])) { // if the session is not yet created, create it now $_SESSION['foodTypes'] = array(); } // check to see if the newly added food type is not already in the array if (in_array($_POST['foodType'], $_SESSION['foodTypes']) === false) { // The selected food item is not in the array // add the selected food item to total food array $_SESSION['foodTypes'][] = $_POST['foodType']; } } echo "Food Added:" . '<br>'; // display the current food list (in a really ugly PHP way) foreach ($_SESSION['foodTypes'] as $food) { echo $food . '<br>'; } } else if (isset($_POST['submit'])) { // check if the session exists, or if its empty if (!isset($_SESSION['foodTypes']) || empty($_SESSION['foodTypes'])) { echo 'No food in the list to submit!'; } else { // Create connection $con=mysqli_connect("localhost","root","","inshapewebsite"); // Check connection if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $clid=111; $date=date("m.d.y"); foreach ($_SESSION['foodTypes'] as $food) { $foodS= $foodS . ",". $food; } $result = mysql_query($con,"INSERT INTO caloriescounter (ID, Date, Lunch) VALUES ('$clid', '$date','$foodS') ON DUPLICATE KEY UPDATE Lunch = '$foodS'"); //mysqli_query($con,"INSERT INTO caloriescounter (ID, Date, Lunch) //VALUES ('$clid', '$date','$foodS')"); mysqli_close($con); // session exists and has content // process the array list here. // after the processing, empty the session $_SESSION['foodTypes'] = array(); } } ?> Link to comment https://forums.phpfreaks.com/topic/277572-check-if-rows-exist-in-mysql-update-if-no-create-new-row/#findComment-1427928 Share on other sites More sharing options...
Barand Posted May 3, 2013 Share Posted May 3, 2013 you used mysql_query() instead of mysqli_query() Link to comment https://forums.phpfreaks.com/topic/277572-check-if-rows-exist-in-mysql-update-if-no-create-new-row/#findComment-1427931 Share on other sites More sharing options...
Yohanne Posted May 3, 2013 Share Posted May 3, 2013 Yes you miss Mysql. Link to comment https://forums.phpfreaks.com/topic/277572-check-if-rows-exist-in-mysql-update-if-no-create-new-row/#findComment-1427987 Share on other sites More sharing options...
farahZ Posted May 3, 2013 Author Share Posted May 3, 2013 thank you guys! Link to comment https://forums.phpfreaks.com/topic/277572-check-if-rows-exist-in-mysql-update-if-no-create-new-row/#findComment-1428030 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.