Jump to content

INSERT INTO members WHERE member_id = '{$_SESSION['SESS_MEMBER_ID']}'


daveoffy

Recommended Posts

I need it to do insert $price, $services, and $date into members where the logged in user is (using ID)

 

I have this

$qry = "INSERT INTO members(service,s_date, price) VALUES('$services','$date','$price')";

 

and want to add this

WHERE member_id = '{$_SESSION['SESS_MEMBER_ID']}'

I get Query Failed. Here is all my code

 

<?php
//Start session
session_start();

//Include database connection details
require_once('config.php');



//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Unable to select database");
}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
	$str = @trim($str);
	if(get_magic_quotes_gpc()) {
		$str = stripslashes($str);
	}
	return mysql_real_escape_string($str);
}

//Sanitize the POST values
if (isset($_POST['service'])) {
    list($services, $price) = explode("|", $_POST['service']);
}
$date = date("m/d/y");
//Create INSERT query
$qry = "INSERT INTO members(service,s_date, price) VALUES('$services','$date','$price') WHERE member_id = '{$_SESSION['SESS_MEMBER_ID']}'";
$result = @mysql_query($qry);

//Check whether the query was successful or not
if($result) {
	header("location: order-success.php");
	exit();
}else {
	die("Query failed");
}
?>

I'm a little confused, are you updating columns on a record that is already there?  ???  (I have to assume you are, because otherwise you wouldn't have a WHERE clause in there testing an id in the member table).

 

If you are trying to INSERT values into a row that already exists on a table, you have to use UPDATE instead.

 

Your code then becomes...

 

$memberid = $_SESSION['SESS_MEMBER_ID'];
$sql = "UPDATE member "
. "SET service='$service',s_date='$date',price=''$price' "
. "WHERE member_id = '$memberid';";
$result = mysql_query($sql);

 

I would also take the value in the $_SESSION superglobal and put it in a $ variable like you did with the VALUES() arguments that you used. (just to make it a little cleaner)

 

In summary, UPDATE is for modifying values in existing rows, INSERT is for creating brand new rows in a table.

 

That help? I goofed up the exact same way a few days ago and it took a little thought before I realized that I was doing it wrong.    ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.