Phear46 Posted May 5, 2013 Share Posted May 5, 2013 // Grab all info into one variable - This is unnecassary but makes it more readable (to me anyway!) $enrollment_array = array('fname' => $_POST['firstname'], 'sname' => $_POST['surname'], 'dob' => $_POST['dob']); $enrollment_array_class = $_POST['class']; //Set DB access variables $host = "localhost"; $user = "***"; $pass = "***"; $db = "userDB"; $table = "students"; //Set Array as variables //Escape for safety $DBfname = mysql_escape_string($enrollment_array['fname']); $DBsname = mysql_escape_string($enrollment_array['sname']); $DBdob = mysql_escape_string($enrollment_array['dob']); $DBclass = mysql_escape_string($enrollment_array_class); //Open Connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); //Select DB mysql_select_db($db) or die ("Unable to select db!"); //Create query $query = "INSERT INTO $table (fname, sname, dob, class) VALUES ('$DBfname', '$DBsname','$DBdob', '$DBclass')"; //Execute query mysql_query($query) or die ("Error in query: $query.".mysql_error()); echo"Added new student!<br />"; //Close connection mysql_close($connection); OK this is the script I have, This is just a small (or not so!) project of mine im using to learn the ins and outs of PHP and mySQL so please forgive me for being a noob! I have a form with 3 text input (fname, sname, dob) and an array 'class[]' of 7 checkboxes. One must be ticked but no more than three. Ive already got all the error checking sorted (mostly) for form inputs but now I'm struggling to figure out how to input my class[] array into my 'class' column in the DB. The 'class' column is of the SET data type and Ive set up the allowed strings correctly as far as i know. When i run the script it gives no errors and even adds a student to the table but the 'class' column is empty. I think I'm going wrong with these two: $DBclass = mysql_escape_string($enrollment_array_class); $query = "INSERT INTO $table (fname, sname, dob, class) VALUES ('$DBfname', '$DBsname','$DBdob', '$DBclass')"; I'm assuming that there is a mysql_escape_array function or something similar? If not i could maybe do a for-each on the array and escape each string one by one into a new array? With the query i think i might need to do something similar to this but im not sure how to write it in php: INSERT fname, sname, dob; select row from table with matching fname, sname, dob; if rows returned = 1 { add class array to class column on row matching returned ID using something like: INSERT INTO $table (class) VALUES ($DBclass); } else { ERROR - either no matching student OR duplicates exist } I know this is asking quite a bit, I'm trying to be as clear as i can. Thanks for reading! Nathan Quote Link to comment https://forums.phpfreaks.com/topic/277653-mysql-insert-data-into-set-colum-type-along-with-a-few-other-things/ Share on other sites More sharing options...
Phear46 Posted May 6, 2013 Author Share Posted May 6, 2013 //Create query's foreach ($enrollment_array_class as $a){ if ($str == ""){ mysql_escape_string($a); $str = $a; } else { mysql_escape_string($a); $str = "$str,$a"; } } $class_string = $str; $query_insert_name = "INSERT INTO $table (fname, sname, dob) VALUES ('$DBfname', '$DBsname','$DBdob')"; $query_select = "SELECT * FROM $table WHERE fname='$DBfname' AND sname='$DBsname' AND dob='$DBdob'"; //Execute querys //Enter name/sname/dob into table mysql_query($query_insert_name) or die ("Error in query: $query_insert_name.".mysql_error()); //Get ID of the row that was just enterd $result = mysql_query($query_select) or die ("Error in query: $query_select.".mysql_error()); while ($row = mysql_fetch_array($result)) { $row_id = $row['id']; } //Enter classes into the row using the row id - set from the query above $query_insert_class = "INSERT INTO $table WHERE id='$row_id' (class) VALUES ('$class_string')"; mysql_query($query_insert_class) or die ("Error in query: $query_insert_class.".mysql_error()); Thought i had this cracked but ive got a problem in that last query, cant figure it out! tried as above and also tried: $query_insert_class = "INSERT INTO $table (class) VALUES ('$class_string') WHERE id='$row_id'"; mysql_query($query_insert_class) or die ("Error in query: $query_insert_class.".mysql_error()); The problem seems to be the WHERE part but im not sure why >.< Anyone know how to fix this? I think the solution is staring me in the face but im drawing a blank here.... Quote Link to comment https://forums.phpfreaks.com/topic/277653-mysql-insert-data-into-set-colum-type-along-with-a-few-other-things/#findComment-1428544 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.