Why won't my variables pass along each page? Php - php

I can't get my variable "quiz_name" passed along several pages in order to make a quiz. After the "quiz_name" variable is sent from question_menu.php as a $_POST[] to quiz_created.php for processing the quiz_created.php is suppose to send the variable back to question_menu.php. What am I doing wrong. To be honest I think I'm approaching it wrong.
Reworded:
quiz_name = "How to score a Basket" which is submitted to question_menu.php as a $_POST['quiz_name']. Then is submitted a to quiz_created.php as a $_POST['quiz_name'] and then submitted back to question_menu.php.
I get an Variable quiz_name undefined error after it is sent back to quesion_menu.
coach_quizzes.php
<head>
<title>Your Quizzes</title>
</head>
<body>
<h1> Current Quizzes </h1>
<form name="submit_button" action="create-quiz.php">
<input type="submit" value="Create Quiz">
</form>
</body>
</html>
submit to -> create_quiz.php
<?php session_start()
?>
<head>
<title>Your Quizzes</title>
</head>
<body>
<h1> Enter Quiz Name </h1>
<form action="questions_menu(test).php" method="post">
<input type="text" name="quiz_name" maxlength="30" size="30">
<input type="submit" value="Create Quiz">
</form>
</body>
</html>
Enter: "How to score a Basket" then submit to question_menu.php
<!DOCTYPE html>
<html>
<head>
<title>Add Question</title>
<link rel="stylesheet" type="text/css" href="css/info_style.css" />
</head>
<body>
<div id="main">
<header>
<div id="welcome">
<h2>Prairie View A&M University</h2>
</div><!--close welcome-->
</header>
<div id="site_content">
<form enctype="multipart/form-data" method="post" action="quiz_created.php">
<table border="0" name"form_table">
<tr>
<td>Quiz Name</td>
<td><?php echo $_POST['quiz_name']?></td>
</tr>
<tr>
<td>Question</td>
<td><textarea name="description" rows="4" cols="50"></textarea></td>
</tr>
<tr>
<td>Option 1</td>
<td><input type="text" name="option1" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Option 2</td>
<td> <input type="text" name="option2" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Option 3</td>
<td><input type="text" name="option3" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Answer</td>
<td>
<select name="dropdown">
<option value='option1'>Option 1</option>
<option value='option2'>Option 2</option>
<option value='option3'>Option 3</option>
</select>
</td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="file" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="10000" /><br>
</td>
</tr>
<tr>
<td colspan="2"><p>
<input type="submit" value="Add Question">
</p></td>
</tr>
</table>
</form>
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "coach_john";
$con = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MYsql");
//echo "Connected to mysql<br>";
mysql_select_db("$database")
or die("Could not select Basketball_database");
//echo "Connected to database";
//update when update button pressed
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]' WHERE question_id='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};//end of if statement
//delete when delete button pressed
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM $table WHERE question_id='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};//end of if statement
$mysql = "SELECT * FROM $table";
$mydata = mysql_query($mysql,$con);
//create table
echo "<table border=1
<tr>
<th>Question ID</th>
<th>Quiz Name</th>
<th>Question Description</th>
<th>Option 1</th>
<th>Option 2</th>
<th>Option 3</th>
<th>Answer</th>
<th>Picture</th>
</tr>";
//insert data into rows
while($records = mysql_fetch_array($mydata)){
echo "<form action=questions_menu(test).php method=post>";
echo "<tr>";
echo "<td>"."<input type=text name=questionID size=5 value=".$records['question_id']." </td>";
echo "<td>"."<input type=text name=option1 size=18 value=".$records['quiz_name']." </td>";
echo "<td>"."<textarea name=description rows=1 cols=25>".$records['question_description']."</textarea>"."</td>";
echo "<td>"."<input type=text name=option1 size=18 value=".$records['option_a']." </td>";
echo "<td>"."<input type=text name=option2 size=15 value=".$records['option_b']." </td>";
echo "<td>"."<input type=text name=option3 size= 15 value=".$records['option_c']." </td>";
echo "<td>"."<input type=text name=answer size=15 value=".$records['answer']." </td>";
echo "<td>". $records['image'] ." </td>";
echo "<td>"."<input type=hidden name=hidden value=".$records['question_id']." </td>";
//update button
echo "<td>"."<input type=submit name=update value=Update onclick='return confirm(\"Are you sure you want to update question?\")'>"." </td>";
//delete button
echo "<td>"."<input type=submit name=delete value=Delete onclick='return confirm(\"Are you sure you want to delete question?\")'>"." </td>";
echo "</tr>";
echo "</form>";//end form
} echo "</table>";
mysql_close();
?> <!-- End of php code-->
</div><!--close site_content-->
<footer>
Home | Photos | Videos | Schedule | Contact<br/><br/>
</footer>
</div><!--close main-->
</body>
</html>
after the information is added submit value= "Add Question" information is sent to quiz_created.php:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "coach_john";
$con = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MYsql");
// echo "Connected to mysql<br>";
$db = mysql_select_db("$database")
or die("Could not select Basketball_database");
//echo "Connected to database";
$mysql = "INSERT INTO $table(question_description, quiz_name, option_a, option_b, option_c, answer) VALUES('$_POST[description]','$_POST[quiz_name]','$_POST[option1]','$_POST[option2]','$_POST[option3]','$_POST[dropdown]')";
if(!mysql_query($mysql))
echo mysql_errno($con) . ": " . mysql_error($con) . "\n";
//die("Disconnected");
$quiz=$_POST['quiz_name'];
//Upload images
if($_POST)
{
if ($_FILES["file"]["error"] > 0)
{
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{ //move_uploaded_file function will upload your image.
if(move_uploaded_file($_FILES["file"]["tmp_name"],"uploaded/" . $_FILES["file"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$image_loc=addslashes (getcwd()."/uploaded"."/".$_FILES["file"]["name"]);
$query_image = "insert into $table (image, img_location) values ('".$_FILES['file']['name']."', '$image_loc')";
if(mysql_query($query_image))
{
echo "Stored in: " . "uploaded/" . $_FILES["file"]["name"];
}
else
{
echo 'File name not stored in database';
}
}
}
}
}
//end of image upload
mysql_close();
?>
<html>
<title>User Added</title>
<body>
<h2>
Question has been added!
</h2>
</body>
<form action="questions_menu(test).php" method="post">
<input type="submit" value="Add Another Question">
<input type="hidden" name="quiz_name" value='<?php echo $quiz;?>'>
</form>
</html>
then finally sent back to question_menu.php but I get an Variable quiz_name undefined error.
I hope I can get help. Thank You.

I see many problems, but one is that you're not escaping your php inserts properly.
echo "<td>"."<input type=text name=questionID size=5 value=".$records['question_id']." </td>";
should be:
echo '<td><input type="text" name="questionID" size="5" value="'.$records['question_id'].'" /></td>';
The way you have it now, you are using the quotes for the echo and there are none for the value="". This is seen all throughout your code. Not sure where you are getting your $records array either, but that's for you to figure out.
These lines are problematic too.
$UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]' WHERE question_id='$_POST[hidden]'";
One major problem is you're trying to set raw POST data into a database. That is a HUGE security flaw. Other problems, like the rest of your php, have to do with quotes and double quotes, semicolons, etc... Read up on syntax. Your whole script is basically wrong.

Related

Edit sql data in php page

I need to edit my database table using submitted data.
This is the form:
mysql_query("set names 'utf8'");
$query = "SELECT * FROM sec1octa";
$result = mysql_query($query);
?>
<div align="center">
<form method="get" action="edit_data.php">
<table width="104" border="1" class="center1">
<tr>
<th width="94">first</th>
<th width="94">second</th>
<th width="94">status</th>
</tr>
<tr>
<?php
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><input type="text" name="id" value="<?php echo $row ['stu_no']; ?> " size=10></td>
<td><input type="text" name="name" value="<?php echo $row ['stu_name']; ?> " size=10></td>
<td><?php
echo '<select name="status">'; {
echo '<option value="open">'.$row['stu_status'].'</option>';
echo '<option value="close">'.prevent.'</option>';
}
echo '</select>';
?></td>
</tr>
<?php
}
}
?>
</tr>
</table>
<input type="submit" name="submit" value="done" />
</form>
The problem is in the edit_data.php page.
I can't UPDATE.
I use this code but it's not working.
require_once('../Connections/config.php');
$id= $_GET['id'];
$status= $_GET['status'];
$query= mysql_query("UPDATE `goh`.`sec1octa` SET `stu_status` = '$status'
WHERE stu_no='".$id."'") or die (mysql_error ());
if($query){echo $status ."done ";}
The reason you are only getting the last values in your edit_data.php $_GET is because you are not setting the input/select names as arrays.
<input type="text" name="id" value="some_stu_no">
is happening over and over and over and every new one overwrites the previous.
Instead, you should use:
<input type="text" name="id[]" value="some_stu_no">
This will allow you to pass multiple id's in a single form submission.
Your form:
<form method="POST" action="edit_data.php">
....
echo "<tr>";
echo "<th>id</th>";
echo "<th>name</th>";
echo "<th>status</th>";
echo "</tr>";
if(mysql_num_rows($result)>0){
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input type=\"text\" name=\"id[]\" value=\"{$row['stu_no']}\" size=\"10\"></td>";
echo "<td>{$row['stu_name']}</td>";
echo "<td>";
echo "<select name=\"status[]\">"; // I don't like your option set up here, but I don't fully understand it either.
echo "<option value=\"open\">{$row['stu_status']}</option>";
echo "<option value=\"close\">.prevent.</option>";
echo "</select>";
echo "</td>";
echo "</tr>";
}
}
....
<input type="submit" value="Submit All">
</form>
edit_data.php
// create a mysqli connection called $db
if(isset($_POST['id'])){
$tally=0;
// build all queries for the batch
foreach($_POST['id'] as $index=>$id){
$queries[]="UPDATE `goh`.`sec1octa` SET `stu_status`='".mysqli_real_escape_string($db,$_POST['status'][$index])."' WHERE `stu_no`='".mysqli_real_escape_string($db,$id)."'";
}
// run all queries
if(mysqli_multi_query($db,implode(';',$queries)){
do{
$tally+=mysqli_affected_rows($db);
} while(mysqli_more_results($db) && mysqli_next_result($db));
}
// assess the outcome
if($error_mess=mysqli_error($db)){
echo "Syntax Error: $error_mess";
}else{
echo "$tally row",($tally!=1?"s":"")," updated";
}
mysqli_close($con);
}

Radio buttons with as-is/update/delete functionality

So I have created a table that has 3 radio buttons. I am having trouble understanding how to get each radio button to perform their named task [function]. Here is a picture of my table:
Can someone explain or give me an example as to how to give functionality to these radio buttons. So that when text is edited and say "update" is selected and the submit button is clicked it would update the database.
Here is my code for this section:
<!DOCTYPE html PUBLIC>
<?php
$connection = mysql_connect('localhost', 'root', 'root');
//Test Connection
if (!$connection)
{
die ("Could not connct to the database: <br />". mysql_error());
}
//select db
mysql_select_db('Assets');
$sql1= "SELECT gid, gstmnt FROM Goals";
$records1 = mysql_query($sql1);
//Adding functions to submit
//$gstmnt_up = $_POST['gstmnt'];
?>
<html>
<head>
<title>List of GOALS/List of SERVICES</title>
</head>
<body>
<table width='800' border='3' cellpadding='2' cellspacing='2'>
<tr>
<h1>List of Goals</h1>
<th>GID</th>
<th width= '200'>Goal Statement</th>
<!--Sumbit column-->
<th width= '85'>
<form id="EditGoalForm" action="EditGoals.php" method="POST">
<input type="submit" name="submit" value="Submit"/>
</form>
</th>
</tr>
<!--This is a paragraph. It is editable. Try to change this text.</p>-->
<?php
while ($gtable = mysql_fetch_assoc($records1)) {
echo "<tr>";
echo "<td>".$gtable['gid']."</td>";
echo "<td><div id=textarea contenteditable>".$gtable['gstmnt']."</div></td>";
echo "<td>
<input type='radio' name=".$gtable['gid']." value='asis' checked> AS-IS </br>
<input type='radio' name=".$gtable['gid']." id='radio_update".$gtable['gid']." value='update' > Update </br>
<input type='radio' name=".$gtable['gid']." value='delete' > Delete
</td>";
echo "</tr>";
}
echo "<td> </td>";
echo "<td>
<input type= 'text name='addGoal' placeholder='ADD NEW GOAL HERE' size='100'>
</td>";
echo "<td>
<input type='checkbox' name='add' value='checked'> Add Goal
</td>";
?>
</table>

keep data in form when clicking submit

EDIT: Apologies - all code now pasted below
Apologies for the first time newbie question - I've been looking for an answer here and on google - I get the feeling this is some simple coding i'm messing up.
I've created a form which, when you validate, you check all the data on the same page and the original data is kept in the original form so you can make changes if you wish.
I have a section of the form where this doesn't work however - where you can ask multiple questions and each question can have up to 4 answers.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
<title>Test page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="stylenewtest.css" type="text/css" />
<script language="javascript" src="tableadd.js"></script>
</head>
<body>
<?php
$string = isset($_POST['quiz_format']) ? $_POST['quiz_format'] : false;
$quiz_format = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $string);
$stringquiz_100 = isset($_POST['quiz_100']) ? $_POST['quiz_100'] : false;
$quiz_100 = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_100);
$stringquiz_fback = isset($_POST['quiz_fback']) ? $_POST['quiz_fback'] : false;
$quiz_fback = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_fback);
$stringquiz_title = isset($_POST['quiz_title']) ? $_POST['quiz_title'] : false;
$quiz_title = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_title);
$stringquiz_instruct = isset($_POST['quiz_instruct']) ? $_POST['quiz_instruct'] : false;
$quiz_instruct = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\?,\"\'\._#\\/ !&()=\+;:\t\r\n-]", "", $stringquiz_instruct);
$stringquiz_time = isset($_POST['quiz_time']) ? $_POST['quiz_time'] : false;
$quiz_time = ereg_replace("[^0-9a-zA-Z?ąćęłńóśżźĄĆĘŁŃÓŚŻŹ\. ]", "", $stringquiz_time);
?>
<div id="content">
<p><span class="error">* required field.</span></p>
<form method="post" action="">
<div class="datagrid">
<table>
<tr><th colspan="2">Please choose the format of the quiz<span class="error">*</span></th></tr>
<tr><td>Hangman</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Hangman") echo "checked";?> value="Hangman"></td></tr>
<tr><td>Gap fill</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Gap fill") echo "checked";?> value="Gap fill"></td></tr>
<tr><td>Multiple choice</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Multiple Choice") echo "checked";?> value="Multiple choice"></td></tr>
<tr><td>Simple Q & A</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Simple Q & A") echo "checked";?> value="Simple Q & A"></td></tr>
<tr><td>Word formation hangman</td><td><input type="radio" name="quiz_format" <?php if (isset($quiz_format) && $quiz_format=="Word formation hangman") echo "checked";?> value="Word formation hangman"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Continue repeating the exercise until 100% achieved?<span class="error">*</span></th></tr>
<tr><td>Yes</td><td><input type="radio" name="quiz_100" <?php if (isset($quiz_100) && $quiz_100=="Yes") echo "checked";?> value="Yes"></td></tr>
<tr><td>No</td><td><input type="radio" name="quiz_100" <?php if (isset($quiz_100) && $quiz_100=="No") echo "checked";?> value="No"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Show the answer as correct/incorrect after every question or just at the end?<span class="error">*</span></th></tr>
<tr><td>After every question</td><td><input type="radio" name="quiz_fback" <?php if (isset($quiz_fback) && $quiz_fback=="After every question") echo "checked";?> value="After every question"></td></tr>
<tr><td>Just at the end</td><td><input type="radio" name="quiz_fback" <?php if (isset($quiz_fback) && $quiz_fback=="Just at the end") echo "checked";?> value="Just at the end"></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<table>
<tr><th colspan="2">Please enter in the title of the quiz<span class="error">*</span></th></tr>
<tr><td>Quiz title:</td><td><input type="text" name="quiz_title" value="<?php echo $_POST[quiz_title];?>"></td></tr>
</table>
<br />
<table>
<tr><th colspan="2">Please enter in the instructions for the quiz<span class="error">*</span></th></tr>
<tr><td>Quiz instructions:</td><td><textarea name="quiz_instruct" rows="10" cols="40"><?php echo $_POST[quiz_instruct];?></textarea></td></tr>
</table>
</div>
<br />
<div class="datagrid">
<script language="javascript">
window.onload = moreFields;
window.onload = init;
</script>
<table>
<tr><th colspan="5">Please enter in the quiz's questions and answers:<span class="error">*</span></th></tr>
</table>
<div id="readroot" style="display: none">
<input type="button" value="Remove a question field"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" />
<table>
<tr>
<td>Question:</td>
<td colspan="3"><input type="text" name="q[]" value="<?php echo $_POST['q'][$i] ?>"></td>
</tr>
<tr>
<td>Answer 1:</td>
<td>Answer 2:</td>
<td>Answer 3:</td>
<td>Answer 4:</td>
</tr>
<tr>
<td><input type="text" name="a1[]" value="<?php echo $_POST['a1'][$i] ?>"></td>
<td><input type="text" name="a2[]" value="<?php echo $_POST['a2'][$i] ?>"></td>
<td><input type="text" name="a3[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
<td><input type="text" name="a4[]" value="<?php echo $_POST['a4'][$i] ?>"></td>
</tr>
</table>
</div>
<span id="writeroot"></span><input type="button" id="moreFields" value="Click here to add further question fields" /><br />
</div>
<br />
<div class="datagrid">
<table>
<tr><td><input type="submit" name="Submit1" value="Submit"/></td></tr>
</table>
</div>
</form>
<?php
if ( isset( $_POST['Submit1'] ) ) {
echo "<form method=post action=insert.php>";
echo "<h1>Please confirm the quiz details are correct</h1>";
echo "<div class=datagrid>";
echo "<table>";
if(empty($string)){
echo("<tr><td>Quiz format:</td><td><h3>Please go back and choose a quiz format<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz format:</td><td>" . $quiz_format . "</td></tr>");
echo("<input type=hidden name=quiz_format value='" . $quiz_format . "' />");
}
if(empty($stringquiz_100)){
echo("<tr><td>Continue repeating the exercise until a 100% score is achieved?:</td><td><h3>Please go back and choose an option<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Continue repeating the exercise until a 100% score is achieved?:</td><td>" . $quiz_100 . "</td></tr>");
echo("<input type=hidden name=quiz_100 value='" . $quiz_100 . "' />");
}
if(empty($stringquiz_100)){
echo("<tr><td>Do you want the answer to be shown after every question or at the end of the quiz?:</td><td><h3>Please go back and choose an option<h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Do you want the answer to be shown after every question or at the end of the quiz?:</td><td>" . $quiz_fback . "</td></tr>");
echo("<input type=hidden name=quiz_fback value='" . $quiz_fback . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
if(empty($stringquiz_title)){
echo("<tr><td>Quiz title:</td><td><h3>Please go back and enter in a title for the quiz</h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz title:</td><td>" . $quiz_title . "</td></tr>");
echo("<input type=hidden name=quiz_title value='" . $quiz_title . "' />");
}
if(empty($stringquiz_instruct)){
echo("<tr><td>Quiz instructions:</td><td><h3>Please go back and enter in instructions for the quiz</h3></td></tr>");
$showbutton=1;
}
else{
echo("<tr><td>Quiz instructions:</td><td>" . $quiz_instruct . "</td></tr>");
echo("<input type=hidden name=quiz_instruct value='" . $quiz_instruct . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
if(empty($stringquiz_time)){
echo("<tr><td>Time limit:</td><td>not specified</td></tr>");
}
else
{
echo("<tr><td>Time limit:</td><td>" . $quiz_time . "</td></tr>");
echo("<input type=hidden name=quiz_time value='" . $quiz_time . "' />");
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
echo "<tr><td colspan=5>The questions and answers to your quiz:</td></tr>";
$aq = $_POST['q'];
$aa1 = $_POST['a1'];
$aa2 = $_POST['a2'];
$aa3 = $_POST['a3'];
$aa4 = $_POST['a4'];
$N = count($aq);
for($i=1; $i < $N; $i++){
if($aq[$i]==""){
echo("<tr><td>Question:</td><td><h3>Please go back and enter a question</h3></td></tr>");
$showbutton=1;
}
elseif($aa1[$i]==""){
echo("<tr><td>Answer:</td><td><h3>Please go back and enter at least one answer</h3></td></tr>");
$showbutton=1;
}
else{
echo ("<table><tr><td>Question:</td><td colspan=3>" . $aq[$i]. "<input type=hidden name=q[] value='" . $aq[$i] . "'></td></tr>
<tr><td>Answer 1:</td><td>Answer 2:</td><td>Answer 3:</td><td>Answer 4:</td></tr>
<tr><td>" .$aa1[$i] . " <input type=hidden name=a1[] value='" .$aa1[$i] . "'></td>
<td>" .$aa2[$i] . " <input type=hidden name=a2[] value='" .$aa2[$i] . "'></td>
<td>" .$aa3[$i] . " <input type=hidden name=a3[] value='" .$aa3[$i] . "'></td>
<td>" .$aa4[$i] . " <input type=hidden name=a4[] value='" .$aa4[$i] . "'></td> </tr></table>");
}
}
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div class=datagrid>";
echo "<table>";
echo "<tr><td><input type=button value='<< Go Back' onclick='goBack()' /></td></tr>";
if ($showbutton =="1"){
}
else{
echo "<tr><td><input type=submit value=Submit></td></tr>";
echo "</table>";
echo "</form>";
}
}
?>
</div>
</body>
</html>
but every time I submit, the questions and answers carry through but the form fields lose all the data. How can I keep the data in the fields?
Many thanks in advance
You can simply set the value of the input to either $_POST['old_value_name] or $_GET['old_value_name'] depending on which form method you are using. Therefore, your new code would look something like this (assuming that you are looking for the key $i from your question):
<table>
<tr>
<td>Question:</td>
<td colspan="3"><input type="text" name="q[]" value="<?php echo $_POST['q'][$i] ?>"></td>
</tr>
<tr>
<td>Answer 1:</td>
<td>Answer 2:</td>
<td>Answer 3:</td>
<td>Answer 4:</td>
</tr>
<tr>
<td><input type="text" name="a1[]" value="<?php echo $_POST['a1'][$i] ?>"></td>
<td><input type="text" name="a2[]" value="<?php echo $_POST['a2'][$i] ?>"></td>
<td><input type="text" name="a3[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
<td><input type="text" name="a4[]" value="<?php echo $_POST['a3'][$i] ?>"></td>
</tr>
</table>
As far as I've understood your problem you want to keep values in a form after they were sent to server. "I've created a form which, when you validate, you check all the data on the same page and the original data is kept in the original form so you can make changes if you wish"
It's ok. I remember I wanted the same a few months ago. This is why I tried to help.
There is only a problem that after pressing "submit" button form is sent to server and page is reloaded.
In return you get a new page from webserver which you pointed in the form field "action".
I would do it so:
Target form: <br/>
<form id="target" action="index.php" method='post'>
<input name="first_field" type="text" value="<?php if ( isset($_POST['first_field']) )
echo $_POST['first_field']; ?>" /> <br/>
<input name="second_field" type="text" value="<?php if ( isset($_POST['second_field']))
echo $_POST['second_field'];?>" /> <br/>
<p><b>What kind of girls do you like?:</b><Br>
<input type="radio" name="browser" value="brunette" <?php if( $_POST['browser']==="brunette" ) echo "checked"; ?> > brunette<Br>
<input type="radio" name="browser" value="blonde" <?php if( $_POST['browser']==="blonde" ) echo "checked"; ?> > blonde<Br>
<input type="radio" name="browser" value="red" <?php if( $_POST['browser']==="red" ) echo "checked"; ?> > red<Br>
</p>
<input type="submit" />
</form>
Any ideas how it can be changed/improved are encouraged.

how to make ascending id in php

this code not asceding id. we have id 1, 2, 3, 15. when adding next record is read id 16. so how to solve this problem ? id must 1,2,3,4,5,etc
<?php
include"class_koneksi.php";
$db=new database();
$db->konek();
if(empty($_GET['aksi'])){
$daftar=$db->tampilData();
echo"<table border=1>
<tr>
<td>Id</td>
<td>Nama</td>
<td>Email</td>
<td>Pesan</td>
<td>Edit</td>
<td>Hapus</td>
</tr>";
foreach($daftar as $data){
echo" <tr>
<td>".$data['id']."</td>
<td>".$data['nama']."</td>
<td>".$data['email']."</td>
<td>".$data['pesan']."</td>
<td><a href='?aksi=edit&id=$data[id]'>edit</a></td>
<td><a href='?aksi=hapus_data&id=$data[id]'>Hapus</a></td>
</tr>";
}
echo"</table> <br> <a href='?aksi=tambah'>TAMBAH</a>";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah'){
echo"<br>
<form method=POST action='?aksi=tambah_data'>
<table>
<tr><td>Nama</td><td><input type=text name='nama'></td></tr>
<tr><td>Email</td><td><input type=text name='email'></td></tr>
<tr><td>Pesan</td><td><input type=text name='pesan'></td></tr>
<tr><td></td><td><input type=submit value='simpan'></td></tr>
</table>
</form>
";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah_data'){
$id= isset($_POST['id']);
$nama=$_POST['nama'];
$email=$_POST['email'];
$pesan=$_POST['pesan'];
$db->tambahData($id,$nama,$email,$pesan);
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='edit'){
$id=$_GET['id'];
echo"<br>
<form method=POST action='?aksi=update_data'>
<table>
<tr><td>Id</td><td><input type=text name='id' value='".$db->bacaData('id',$id)."'></td></tr>
<tr><td>Nama</td><td><input type=text name='nama' value='".$db->bacaData('nama',$id)."'></td></tr>
<tr><td>Email</td><td><input type=text name='email' value='".$db->bacaData('email',$id)."'></td></tr>
<tr><td>Pesan</td><td><input type=text name='pesan' value='".$db->bacaData('pesan',$id)."'></td></tr>
<tr><td></td><td><input type=submit value='simpan'></td></tr>
</table>
<input type='hidden' name='id' value='".$db->bacaData('id',$id)."'>
</form>
";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='update_data'){
$id=$_POST['id'];
$nama=$_POST['nama'];
$email=$_POST['email'];
$pesan=$_POST['pesan'];
$db->updateData($id,$nama,$email,$pesan);
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='hapus_data'){
$id=$_GET['id'];
$db->hapusData($id);
}
?>
can you tell me where this code wrong?
can you help me solve this problem? thanks
Remove the auto increment property of the table which is showing 16.
Before inserting a new row in that table write a select query.
$sql = "select * from `table_name`";
$result = mysql_query($sql);
$id=count($result)+1;
if(count($result)>0){
$temp = 1;
while($row = mysql_fetch_assoc($result)){
if($row['id']!=$temp++){
$id=$temp; break;
}
}
}
$sql = "insert into `table_name` (`id`,`..`,`..`) values('"$id.."','..','..')";
mysql_query($sql);
I hope this will solve your query...
It is showing id as 16 becouse in table properties Id is set as auto increment
First remove auto increment from id in the respective database table you are having this issue. Then Replace this code section with your code.
.
.
.
elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah'){
$daftar=$db->tampilData();
$id=count($daftar)+1;
if(count($daftar)>0){
$temp = 1;
forech($daftar as $row){
if($row['id']!=$temp++){
$id=$temp; break;
}
}
}
echo"<br>
<form method=POST action='?aksi=tambah_data'>
<table>
<tr><td>Nama</td><td><input type=text name='nama'><input type="hidden" name='id' value="<?php echo $id ?>"></td></tr>
<tr><td>Email</td><td><input type=text name='email'></td></tr>
<tr><td>Pesan</td><td><input type=text name='pesan'></td></tr>
<tr><td></td><td><input type=submit value='simpan'></td></tr>
</table>
</form>
";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah_data'){
$id= $_POST['id'];
$nama=$_POST['nama'];
$email=$_POST['email'];
$pesan=$_POST['pesan'];
$db->tambahData($id,$nama,$email,$pesan);
}
.
.
.
I hope this solves your requirement..

PHP mysql form , STUCK

Ok please if anyone could help with this simple php and mysql question.
I'm new to databases, and trying to create my first user-friendly database. but i ran in to small problem.
I have a page that displays data, Than I also have a page where same data is displayed along with edit link, that goes to the edit form. However in the edit form the info is not coming up.
Here is my "edit.php" it shows info from database along with edit link:
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("beyondmotors", $con);
$result = mysql_query("SELECT * FROM vehicles");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Year</th>
<th>Make</th>
<th>Model</th>
<th>Mileage</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['year'] . "</td>";
echo "<td>" . $row['make'] . "</td>";
echo "<td>" . $row['model'] . "</td>";
echo "<td>" . $row['mileage'] . "</td>";
echo ("<td>Edit</td></tr>");
echo "</tr>";
}
echo "</table>";
mysql_close($con);
And here is the Edit form :
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table>
<?
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("beyondmotors", $con);
$order = "SELECT * FROM vehicles where id=$id";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<? echo "$row[id]"?>">
<tr>
<td>Year</td>
<td>
<input type="text" name="year"
size="20" value="<? echo "$row[year]"?>">
</td>
</tr>
<tr>
<td>Make</td>
<td>
<input type="text" name="make"
size="20" value="<? echo "$row[make]"?>">
</td>
</tr>
<tr>
<td>Model</td>
<td>
<input type="text" name="model"
size="20" value="<? echo "$row[model]"?>">
</td>
</tr>
<tr>
<td>Mileage</td>
<td>
<input type="text" name="mileage"
value="<? echo "$row[mileage]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
Than I also have Edit data, but it doesnt even get to that. Here is what i get at the edit form!! http://s562.photobucket.com/albums/ss69/intensemx/?action=view&current=pic.jpg
I followed this tutorial :
Help i'm lost here, Thanks in advance :)
Can any one recommend good place where I can get a similar tutorial.
The tutorial you are using is assuming that short tags are turned on. Alter the code to use <?php instead.
It also suffers from an SQL injection vulnerability.
It also appears to assume that register_globals is turned on.
Do not use that tutorial. It is dangerously bad.
You need to enclose your array keys in quotes. For example, this:
<input type="hidden" name="id" value="<? echo "$row[id]"?>">
Should be this:
<input type="hidden" name="id" value="<? echo $row['id']?>">
Now, besides that, please consider the problems mention by Quentin in his answer.
Your edit form uses PHP Short Tags (<? ?>) which are probably not enabled on your server, so everything looks wrong.
Your options to fix it are to 1) Enable short tags, or 2) change <? to <?php in your edit file.

Categories