yobo Posted March 13, 2007 Share Posted March 13, 2007 hey all, i am having problems inserting data into 2 tables at the same time after submitting my form here is my form code <?php session_start(); if (!isset($_COOKIE['loggedin'])) die("You are not logged in!"); ?> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Add a Hack</title> </head> <?php $userid=$_GET['userid']; $_SESSION['userid']=$userid; include("config.php"); // connect to the mysql server $link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error()); // select the database mysql_select_db($database) or die ("Could not select database because ".mysql_error()); $type = @mysql_query('SELECT typeid, name FROM type'); if(!$type) { exit('<p>unable to obtain category list from the ' . 'databas.</p>'); } ?> <body> <h2> Submit a New Hack!</h2> <form action="hackadded.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="userid" value="<?php echo $userid;?>" /><br/> <label> Hack Catagory: <select name="tid" size="1"> <option selected value="">select one</option> <option value="">---------</option><br/> <?php while ($hacktype = mysql_fetch_array($type)) { $tid = $hacktype['typeid']; $aname = htmlspecialchars($hacktype['name']); echo "<option value='$tid'>$aname</option>\n"; } ?> </select><br/> <label> Hack Name: <input type="text" name="hackname" /><br/></label> <label>Upload Hack: <input type="file" name="upload" /><br/></label> <label>Hack Description: <input type="text" name="desc" maxlength="255" /><br/></label> <label>Hack Version: <input type="text" name="version" /><br/></label> <input type="submit" value="SUBMIT HACK" </form> <?php echo " <a href=members.php?userid=$userid>Back To Profile<br>"; ?> </body> </html> and this is the code that handles the form submission. <?php include("config.php"); // connect to the mysql server $link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error()); // select the database mysql_select_db($database) or die ("Could not select database because ".mysql_error()); //bail out if the file isnt really an upload if (!is_uploaded_file($_FILES['upload']['tmp_name'])) { exit('there was no file uploaded!'); } $hackname = $_POST['hackname']; $uploadfile = $_FILES['upload']['tmp_name']; $uploadname = $_FILES['upload']['name']; $uploadtype = $_FILES['upload']['type']; $uploaddesc = $_POST['desc']; $hackversion = $_POST['version']; $userid = $_POST['userid']; $tid = $_POST['tid']; if ($tid == '') { exit('<p>you must select a catagory.</p>'); } //open file for binary reading $tempfile = fopen($uploadfile, 'rb'); //read the entire file into memory using php's //filesize function to get the file size $filedata = fread($tempfile, filesize($uploadfile)); //prepaer for database insert by adding backslashes //before speaciail chractors $filedata = addslashes($filedata); //close connection fclose($tempfile); //create the sql query $sql = "INSERT INTO hacks SET filedata = '$filedata', mimetype = '$uploadtype', hackname = '$hackname', description = '$uploaddesc', version = '$hackversion', username = '$userid'"; $sql2 = "INSERT INTO hacktype SET hacksid = '$hid', typeid = '$tid'"; //perform the insert $ok = @mysql_query($sql); if (!$ok) { exit('database error: ' . mysql_error()); }else{ echo"hack added"; } $hid = mysql_insert_id(); now the data insertes into the hacks table but not the hacktype table what i need is the hacks id and the selected catagory id to be inserted into the hacktype table Link to comment https://forums.phpfreaks.com/topic/42491-having-trouble-inserting-data-into-2-tables/ Share on other sites More sharing options...
only one Posted March 13, 2007 Share Posted March 13, 2007 $ok = @mysql_query($sql); $ok2 = @mysql_query($sql2); if (!$ok2|!$ok) { exit('database error: ' . mysql_error()); }else{ echo"hack added"; } $hid = mysql_insert_id(); Link to comment https://forums.phpfreaks.com/topic/42491-having-trouble-inserting-data-into-2-tables/#findComment-206173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.