bumdeal2 Posted April 2, 2010 Share Posted April 2, 2010 This php code inserts the values into the database <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="server2go"; // Database name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $quesid= $_POST['quesid']; $question= $_POST['question']; $answer= $_POST['answer']; $ranswer = $_POST['ranswer']; $ranswer1= $_POST['ranswer1']; $ranswer2= $_POST['ranswer2']; $qzid= $_POST['qzid']; $next_id=$_REQUEST['next_id']; if(($quesid=="")||($quesid==0)) { $quesid=1; } else { $quesid=$quesid+1; } mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $query = "INSERT INTO questiona (quesid,question,answer,ranswer,ranswer1,ranswer2,qzid) VALUES ('$quesid','$question','$answer','$ranswer','$ranswer1','$ranswer2','$qzid')"; mysql_query($query) or die('Error1, insert query failed'); $query = "FLUSH PRIVILEGES"; mysql_query($query) or die('Error2, insert query failed'); $result = @mysql_query($query); if ($result) { header("location: addquizquestion.php?quesid='$quesid'"); exit(); } ?> This page has the fields ..... that allow the user to enter in the data <table width="400" border="0" cellspacing="1" cellpadding="2"> <form method=post name=form action="createquizcheck1.php?quesid='.$quesid.'" onSubmit="return validate_form ( );"> <input type=hidden name=todo value=post> <tr> <td width="100">Question</td> <td><input name="question" type="text" id="question"></td> </tr> <tr> <td width="100">Right answer</td> <td><input name="answer" type="text" id="answer"></td> </tr> <tr> <td width="100">Wrong Answer</td> <td><input name="ranswer" type="text" id="ranswer"></td> </tr> <tr> <td width="100">Wrong Answer</td> <td><input name="ranswer1" type="text" id="ranswer1"></td> </tr> <tr> <td width="100">Wrong Answer</td> <td><input name="ranswer2" type="text" id="ranswer2"></td> </tr> </select> </td> </tr> <tr> <td width="100"> </td> <td><input name="add" type="submit" id="add" value="add quiz"></td> What i wanted to know is how do u make it so that the value quesid automatically passes through the php code and increments each time.Please could you help me. Should i use (if(isset($_GET['quesid'])) $quesid = $_GET['quesid'] and if so how do i use it in the script.Thank you. Link to comment https://forums.phpfreaks.com/topic/197375-passing-values-from-one-php-form-to-another-and-incrementing/ Share on other sites More sharing options...
KevinM1 Posted April 2, 2010 Share Posted April 2, 2010 For a table id, the best way to do it is to set the column to unsigned int auto-increment not null. This has the column start at 1, and automatically increment on each insert. Link to comment https://forums.phpfreaks.com/topic/197375-passing-values-from-one-php-form-to-another-and-incrementing/#findComment-1035967 Share on other sites More sharing options...
bumdeal2 Posted April 2, 2010 Author Share Posted April 2, 2010 i already have a table with a quest id that auto increments but i need the quesid to increment to ten and then go to another php page, The reason for this is i have a qzid as well so that when u click on the quiz question 1 then 2 then 3 will come up to ten, so i cant auto increment in the mysql database i need to have it in the php form. Link to comment https://forums.phpfreaks.com/topic/197375-passing-values-from-one-php-form-to-another-and-incrementing/#findComment-1035970 Share on other sites More sharing options...
bumdeal2 Posted April 2, 2010 Author Share Posted April 2, 2010 <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="server2go"; // Database name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); if(isset($_POST['add'])) { if(isset($_GET['quesid'])) { $quesid = $_GET['quesid']; header("location: createquizcheck1.php?quesid=$quesid"); exit(); } } ?> <table width="400" border="0" cellspacing="1" cellpadding="2"> <form method=post name=form action="" onSubmit="return validate_form ( );"> <input type=hidden name=todo value=post> <tr> <td width="100">Question</td> <td><input name="question" type="text" id="question"></td> </tr> <tr> <td width="100">Right answer</td> <td><input name="answer" type="text" id="answer"></td> </tr> <tr> <td width="100">Wrong Answer</td> <td><input name="ranswer" type="text" id="ranswer"></td> </tr> <tr> <td width="100">Wrong Answer</td> <td><input name="ranswer1" type="text" id="ranswer1"></td> </tr> <tr> <td width="100">Wrong Answer</td> <td><input name="ranswer2" type="text" id="ranswer2"></td> </tr> </select> </td> </tr> <tr> <td width="100"> </td> <td><input name="add" type="submit" id="add" value="addquiz"></td> and this is my other script <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="server2go"; // Database name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $question= $_POST['question']; $answer= $_POST['answer']; $ranswer = $_POST['ranswer']; $ranswer1= $_POST['ranswer1']; $ranswer2= $_POST['ranswer2']; $qzid= $_POST['qzid']; if(isset($_GET['quesid'])) { $quesid = $_GET['quesid']; if((quesid==0)||(quesid=="")) { $quesid=1; } else { $quesid=$quesid+1; } mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $query = "INSERT INTO questiona (quesid,question,answer,ranswer,ranswer1,ranswer2,qzid) VALUES ('$quesid','$question','$answer','$ranswer','$ranswer1','$ranswer2','$qzid')"; mysql_query($query) or die('Error1, insert query failed'); $query = "FLUSH PRIVILEGES"; mysql_query($query) or die('Error2, insert query failed'); $result = @mysql_query($query); if ($result) { header("location: addquizquestion.php?quesid=$quesid"); exit(); } } ?> Please dont be too harsh im a newbie ,:-) thanks for your time. Link to comment https://forums.phpfreaks.com/topic/197375-passing-values-from-one-php-form-to-another-and-incrementing/#findComment-1036032 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.