Jump to content

check if rows exist in mysql, update. if no, create new row


farahZ
Go to solution Solved by Barand,

Recommended Posts

hello
i 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 exsists
i.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
Share on other sites

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 by ignace
Correct \ to /
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.