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(); Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted May 3, 2013 Solution 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' Quote Link to comment 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, Quote Link to comment Share on other sites More sharing options...
farahZ Posted May 3, 2013 Author Share Posted May 3, 2013 (edited) 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(); } } ?> Edited May 4, 2013 by ignace Correct \ to / Quote Link to comment 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() Quote Link to comment Share on other sites More sharing options...
Yohanne Posted May 3, 2013 Share Posted May 3, 2013 Yes you miss Mysql. Quote Link to comment Share on other sites More sharing options...
farahZ Posted May 3, 2013 Author Share Posted May 3, 2013 thank you guys! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.