daveoffy Posted November 16, 2008 Share Posted November 16, 2008 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']}' Link to comment https://forums.phpfreaks.com/topic/132902-insert-into-members-where-member_id-_sessionsess_member_id/ Share on other sites More sharing options...
refiking Posted November 16, 2008 Share Posted November 16, 2008 Try this $qry = mysql_query("INSERT INTO members(service,s_date, price) VALUES('$services','$date','$price') WHERE member_id = '{$_SESSION['SESS_MEMBER_ID']}'"); Link to comment https://forums.phpfreaks.com/topic/132902-insert-into-members-where-member_id-_sessionsess_member_id/#findComment-691081 Share on other sites More sharing options...
daveoffy Posted November 16, 2008 Author Share Posted November 16, 2008 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"); } ?> Link to comment https://forums.phpfreaks.com/topic/132902-insert-into-members-where-member_id-_sessionsess_member_id/#findComment-691084 Share on other sites More sharing options...
Tantalus Posted November 17, 2008 Share Posted November 17, 2008 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. Link to comment https://forums.phpfreaks.com/topic/132902-insert-into-members-where-member_id-_sessionsess_member_id/#findComment-691798 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.