Talon21 Posted July 7, 2008 Share Posted July 7, 2008 Hi, I'm new to MYSQL I want to insert a PHP variables into MYSQL table the problem is it's insert the row each time the page is loaded the code is: <?php include_once ('config.inc.php'); $pageurl = '/'; $mainKeyword = 'Main Keyword'; $title = 'title'; $keywords = 'keywords'; $description = 'description'; $header = 'header'; $site_group ='sitegroup'; //include secleted template include('templates/main.php'); //insert variables into mysql mysql_select_db("$dbname", $con); $insert = "INSERT INTO convertech VALUES ('mysql_insert_id($id),'$pageurl','$mainKeyword','$title','$keywords','$description','$header','$site_group',)"; $retrieveID = mysql_insert_id(); mysql_query($insert); mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
gijew Posted July 7, 2008 Share Posted July 7, 2008 I could go ahead and assume what it is you're trying to do but I'll just leave it as this... As long as your query functions you're always going to insert a new row in the database table with the above code. What is it you want to happen? Only insert once per user visit? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 7, 2008 Share Posted July 7, 2008 you can't use mysql_insert_id() if u don't have a previous "insert" to get an ID from an auto incremeted key. Quote Link to comment Share on other sites More sharing options...
Talon21 Posted July 7, 2008 Author Share Posted July 7, 2008 I want two things: 1. to insert a row into the database 2. to create a unique id for the row, so once the variable will change it will automatically update the existing row. Quote Link to comment Share on other sites More sharing options...
gijew Posted July 7, 2008 Share Posted July 7, 2008 Just store the session_id() into a session as well as the row id. If it matches via some conditional you write it will either insert/update the database. This is far from exact but it at least demonstrates a possible way this could happen. Basically just throw the user session id in there and compare to it. It's NOT an exact science and there can be all sorts of ways this couldn't work but it gets the job done right now <?php session_start(); // get the user session and store it right away $_SESSION['user_session'] = session_id(); // check the database to see if that session exists $sql = mysql_query("select session_id from convertech where session_id = '".mysql_real_escape_string($_SESSION['user_session'])."'"); // now see if the above query came back with anything // doesn't exist so create a new row if (mysql_num_rows($sql) == 0) { $sql_insert = mysql_query("insert into table values whatever"); // does exist so update } else { $sql_update = mysql_query("update table set whatever = whatever"); } ?> 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.