Multiple post data to Mysql DB - php

I have problem with: I want to create admin for add questions to quiz system. Structure is:
<label>Question 1</label>
<input type='text' name='question' value=''/>
<label>Possible reply</label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>**Correct reply</label>
<input type='text' name='correct' />
<label>Question 2 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 3 </label>
...
<input type='submit' name='submit' value='submit'>
And I need multiple questions post to Mysql db tables: question, 1,2,3,4,5,6, correct.
I was create this:
<?php
if(isset($_POST['submit']))
{
$question $_POST['question '];
$a = $_POST['1'];
$b = $_POST['2'];
$c = $_POST['3'];
$d = $_POST['4'];
$e = $_POST['5'];
$f = $_POST['6'];
correct = $_POST['correct '];
$result=mysql_query("insert into test (question, 1, 2, 3, 4, 5, 6, correct) values ('$result', '$a', '$b', '$c', '$d', '$e', '$f', '$correct' )");
}
else
{
?>
<label>Question 1 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 2 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 3 </label>
...
<input type='submit' name='submit' value='submit'>
<?
}
But this send only 1 question to DB.

If you can't use
<input type='text' name='question_1' value=''/>
<input type='text' name='question_1' value=''/>
<input type='text' name='question_1' value=''/>
Write in HTML:
<input type='text' name='question[]' value=''/>
<input type='text' name='reply1[]' />
<input type='text' name='reply2[]' />
<input type='text' name='reply3[]' />
<input type='text' name='reply4[]' />
<input type='text' name='reply5[]' />
<input type='text' name='reply6[]' />
<input type='text' name='correct[]' />
In PHP:
<?php
if (isset($_POST['submit'])){
$questions=$_POST['question'];
$reply1=$_POST['reply1'];
$reply2=$_POST['reply2'];
$reply3=$_POST['reply3'];
$reply4=$_POST['reply4'];
$reply5=$_POST['reply5'];
$reply6=$_POST['reply6'];
$correct=$_POST['correct'];
foreach($questions as $key=>$value){
$result=mysql_query("insert into test (question, 1, 2, 3, 4, 5, 6, correct) values ('$value', '".$reply1[$key]."', '".$reply2[$key]."', '".$reply3[$key]."', '".$reply4[$key]."', '".$reply5[$key]."', '".$reply5[$key]."', '".$correct[$key]."' )");
}
}

It would be nice if you can state your database structure and expected result, because it may not work the way you think and we'll never know until we see it.
If you want 1 answer per row, you need to use a multiple row insertion - How to insert multiple rows in single insert statement?.

try that:
<input type='text' name='question1' value=''/>
<input type='text' name='question2' value=''/>
instead of
<input type='text' name='question1' value=''/>
<input type='text' name='question1' value=''/>

Related

How to redirect a form to the same page while it's being echoed out

I'm trying to redirect a form to the same page typically when a form is displayed to the screen using HTML you can just nest PHP into the form like this
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
but I'm echoing out a form in this use case and I'm receiving errors due to the apostrophes and I can't figure out how to get it working
echo "
<h1>Update '{$rowObj->first_name}'</h1>
<form id='updateuser' class='update' action='echo htmlspecialchars($_SERVER['PHP_SELF'])' method='post'>
<p>Customer ID<input type='text' class='update' name='customer_id' value='$customer_id' readonly /></p>
<p>Name <input type='text' class='update' name='first_name' size='50' value='{$rowObj->first_name}' /></p>
<p>surname <input type='text' class='update' name='surname' size='50' value='{$rowObj->surname}' /></p>
<p>Postcode <input type='text' class='update' name='postcode' size='50' value='{$rowObj->postcode}' /></p>
<p>Address <input type='text' class='update' name='address' size='50' value='{$rowObj->address}' /></p>
<p>Address <input type='text' class='update' name='email' size='50' value='{$rowObj->email}' /></p>
<p>Phone number <input type='text'class='update' name='number' value='{$rowObj->phonenumber}' /></p>
<p><input type='submit' class='submitbutton' name='submit' value='Confirm edit'></p> ";
error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\test\edit.php on line 38
If you're including variables inside of a string, you should remove the quotes used to reference the member of the variable from $_SERVER["PHP_SELF"] to $_SERVER[PHP_SELF].
The first one is fine since it is not included inside of a string:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
That should resolve the errors you're receiving:
echo "
<h1>Update '{$rowObj->first_name}'</h1>
<form id='updateuser' class='update' action='echo htmlspecialchars($_SERVER[PHP_SELF])' method='post'>
<p>Customer ID<input type='text' class='update' name='customer_id' value='$customer_id' readonly /></p>
<p>Name <input type='text' class='update' name='first_name' size='50' value='{$rowObj->first_name}' /></p>
<p>surname <input type='text' class='update' name='surname' size='50' value='{$rowObj->surname}' /></p>
<p>Postcode <input type='text' class='update' name='postcode' size='50' value='{$rowObj->postcode}' /></p>
<p>Address <input type='text' class='update' name='address' size='50' value='{$rowObj->address}' /></p>
<p>Address <input type='text' class='update' name='email' size='50' value='{$rowObj->email}' /></p>
<p>Phone number <input type='text'class='update' name='number' value='{$rowObj->phonenumber}' /></p>
<p><input type='submit' class='submitbutton' name='submit' value='Confirm edit'></p> ";
echo "
<h1>Update '{$rowObj->first_name}'</h1>
<form id='updateuser' class='update' action='echo
htmlspecialchars($_SERVER["PHP_SELF"])' method='post'>
<p>Customer ID<input type='text' class='update'
name='customer_id' value='$customer_id' readonly /></p>
<p>Name <input type='text' class='update' name='first_name'
size='50' value='{$rowObj->first_name}' /></p>
<p>surname <input type='text' class='update' name='surname'
size='50' value='{$rowObj->surname}' /></p>
<p>Postcode <input type='text' class='update' name='postcode'
size='50' value='{$rowObj->postcode}' /></p>
<p>Address <input type='text' class='update' name='address'
size='50' value='{$rowObj->address}' /></p>
<p>Address <input type='text' class='update' name='email'
size='50' value='{$rowObj->email}' /></p>
<p>Phone number <input type='text'class='update' name='number'
value='{$rowObj->phonenumber}' /></p>
<p><input type='submit' class='submitbutton' name='submit'
value='Confirm edit'></p> ";

How to get the data permanently recieved by POST method in a file?

Good day experts!!!
In the code below, the copy.php file will create another php base on input values and will send input values on the newly created php file. On the same page, at first the codes works well but if I open the newly created php file directly, it will give an error PHP Warning: Undefined array key "subject/etc.......). How do I put the values permanently on the post section of the newly created file replacing the post code?
Here's my code below:
copy.php
<?php
$file = 'data.php';
$newfile = $_POST["newFileName"].'.php';
if (!copy($file, $newfile)) {
echo "failed to copy";
}else {
echo "<center><form action='$newfile' method='post'>
Name of the exam: <br><input type='text' name='subject' required><br><br>
Instruction: <br><input type='text' name='instruction' required><br><br>
Questions:<br>
<input type='text' name='new_1'><br>
<input type='text' name='new_2'><br>
<input type='text' name='new_3'><br>
<input type='text' name='new_4'><br>
<input type='text' name='new_5'><br><br>
<input type='submit' value='Create Quiz'>
</form></center>";
}
?>
newfile.php
<?php
$subject=$_POST['subject'];
$instruction=$_POST['instruction'];
$q1=$_POST['new_1'];
$q2=$_POST['new_2'];
$q3=$_POST['new_3'];
$q4=$_POST['new_4'];
$q5=$_POST['new_5'];
echo
"<form action='' method='post' onsubmit='alert('SUBMITTED SUCCESFULLY!')'>
Name: <input type='text' name='NAME' required><br><br>
$subject <br><br>
$instruction <br><br>
<input type='text' name='1' required> $q1 <br>
<input type='text' name='2' required> $q2 <br>
<input type='text' name='3' required> $q3 <br>
<input type='text' name='4' required> $q4 <br>
<input type='text' name='5' required> $q5 <br><br>
<input type='submit' value='submit' onclick='return Confirm();'>
</form>";
?>
You can't open newfile.php directely and have the answers.
The copy file call newfile with the form. newfile expect to receive POST datas. If you open directly newfile, you don't have POST data because no datas where sent.
What you can do :
In copy PHP start a session (session_start()) and give a name to the submit button : <input type='submit' value='Create Quiz' name='subForm'>
In newfile.php you can have something like that :
<?php
session_start();
if(isset($_POST['subForm'])){
$_SESSION['copyForm'] = serialize($_POST);
}
$myForm = unserialize($_SESSION['copyForm']);
$subject=$myForm['subject'];
$instruction=$myForm['instruction'];
$q1=$myForm['new_1'];
$q2=$myForm['new_2'];
$q3=$myForm['new_3'];
$q4=$myForm['new_4'];
$q5=$myForm['new_5'];
echo
"<form action='' method='post' onsubmit='alert('SUBMITTED SUCCESFULLY!')'>
Name: <input type='text' name='NAME' required><br><br>
$subject <br><br>
$instruction <br><br>
<input type='text' name='1' required> $q1 <br>
<input type='text' name='2' required> $q2 <br>
<input type='text' name='3' required> $q3 <br>
<input type='text' name='4' required> $q4 <br>
<input type='text' name='5' required> $q5 <br><br>
<input type='submit' value='submit' onclick='return Confirm();'>
</form>";
?>

Why is my PHP string variable breaking where there is a space? [duplicate]

This question already has answers here:
How to set HTML value attribute (with spaces)
(6 answers)
Closed 1 year ago.
My PHP variable 'contractorcompany' usually has spaces in its value but when I try to use the value in a form it breaks at the space.
PHP
<?php
include "msbkeys.php";
if(isset($_POST['editcontractor']))
{
$contractorid = $_POST['contractorid'];
$contractorfirstname = $_POST['contractorfirstname'];
$contractorlastname = $_POST['contractorlastname'];
$contractorcompany = $_POST['contractorcompany'];
$contractoremail = $_POST['contractoremail'];
$contractorphone = $_POST['contractorphone'];
echo "$contractorcompany";
echo "<form method='POST'>
<input type='hidden' name='contractorid' value=" .$contractorid. ">
First name: <input type='text' name='firstname' value=" .$contractorfirstname. ">
<br/>
Last name: <input type='text' name='lastname' value=" .$contractorlastname. ">
<br/>
Company: <input type='text' name='company' value='".$contractorcompany."'>
<br/>
Email: <input type='text' name='email' value=" .$contractoremail. ">
<br/>
Phone: <input type='text' name='phone' value=" .$contractorphone. ">
<br/>
<input type='submit' name='updatecontractor' value='Submit'>
</form>";
}
mysqli_close($db); // Close connection
?>
When this value is for example 'Grant Grouting', it breaks as per line 6 below.
<form method="post">
<input type="hidden" name="contractorid" value="3">
<input type="hidden" name="contractorfirstname" value="Tom">
<input type="hidden" name="contractorlastname" value="Grant">
<input type="hidden" name="contractoremail" value="tomgrant#grouting.com">
<input type="hidden" name="contractorcompany" value="Grant" grouting>
<input type="hidden" name="contractorphone" value="">
<input name="editcontractor" type="submit" value="Edit this contractor">
</form>
I've tried every combination of quotation marks around the variable but am still not getting the full string passed through the form.
You just missed the quotes for the values:
<?php
include "msbkeys.php";
if(isset($_POST['editcontractor']))
{
$contractorid = $_POST['contractorid'];
$contractorfirstname = $_POST['contractorfirstname'];
$contractorlastname = $_POST['contractorlastname'];
$contractorcompany = $_POST['contractorcompany'];
$contractoremail = $_POST['contractoremail'];
$contractorphone = $_POST['contractorphone'];
echo "$contractorcompany";
echo "<form method='POST'>
<input type='hidden' name='contractorid' value='" .$contractorid. "'>
First name: <input type='text' name='firstname' value='" .$contractorfirstname. "'>
<br/>
Last name: <input type='text' name='lastname' value='" .$contractorlastname. "'>
<br/>
Company: <input type='text' name='company' value='".$contractorcompany."'>
<br/>
Email: <input type='text' name='email' value='" .$contractoremail. "'>
<br/>
Phone: <input type='text' name='phone' value='" .$contractorphone. "'>
<br/>
<input type='submit' name='updatecontractor' value='Submit'>
</form>";
}
mysqli_close($db); // Close connection
?>
Try to sanitize the output with addslashes for example.

php mail form doesn't send mail

I seem to have a problem with an email form I am making.
the form worked correctly until I added some extra parts to it. however, even when I comment my changes, it won't work.
I thought the problem might be due to some syntax error, but It doesn't seem to be so.
I do not get any error message
below is my code:
<?php
if(isset($_REQUEST['email'])) {
$num=$_REQUEST['num'];
$desc=$_REQUEST['desc'];
$subject="einak";
$nomr=$_REQUEST['nomr'];
$sphr=$_REQUEST['sphr'];
$cylr=$_REQUEST['cylr'];
$angr=$_REQUEST['angr'];
$addr=$_REQUEST['addr'];
$diar=$_REQUEST['diar'];
$noml=$_REQUEST['noml'];
$sphl=$_REQUEST['sphl'];
$cyll=$_REQUEST['cyll'];
$angl=$_REQUEST['angl'];
$addl=$_REQUEST['addl'];
$dial=$_REQUEST['dial'];
$type=$_REQUEST['type'];
$matter=$_REQUEST['matter'];
$color=$_REQUEST['color'];
$message=$num."<br />".$nomr."<br/>".$sphr."<br/>".$cylr."<br/>".$angr."<br />".$addr."<br />".$diar."<br />"."<br />".$noml."<br/>".$sphl."<br/>".$cyll."<br/>".$angl."<br />".$addl."<br />".$dial."<br />".$desc."<br />".$type."<br />".$matter."<br />".$color."<br />";
$email='info#toosoptic.ir';
$headers='MIME-Version: 1.0'."\r\n";
$headers.='Content-type: text/html; charset=iso-8859-1'."\r\n";
mail("mail#example.com",$subject,$message,$headers);
echo "ارسال شما موفقیت آمیز بود";
} else {
echo "<form method='post'>
<input name='num' id='num' type='text' /> <br />
<input type='text' name='nomr' id='nomr' > <br />
<input type='text' name='sphr' id='sphr'> <br />
<input type='text' name='cylr' id='cylr' > <br />
<input type='text' name='angr' id='angr' > <br />
<input type='text' name='addr' id='addr' > <br />
<input type='text' name='diar' id='diar' > <br />
<input type='text' name='noml' id='noml' > <br />
<input type='text' name='sphl' id='sphl'> <br />
<input type='text' name='cyll' id='cyll' > <br />
<input type='text' name='angl' id='angl' > <br />
<input type='text' name='addl' id='addl' > <br />
<input type='text' name='dial' id='dial' > <br />
<textarea id='desc' name='desc'> </textarea>
<input name='type' type='radio' value='bifocal' id='type1' />
<input name='type' type='radio' value='progres' id='type2' />
<input name='type' type='radio' value='single' id='type3' />
<input name='type' type='radio' value='decen' id='type4' />
<input name='matter' type='radio' value='glass' id='mat1' />
<input name='matter' type='radio' value='plastic' id='mat2' />
<input name='color' type='radio' value='single' id='col1' />
<input name='color' type='radio' value='decen' id='col2' />
<input type='submit' id='sb' value='ارسال اطلاعات' />
<input type='text' name='name' id='name' > <br />
<input type='add' name='address' id='address' > <br />
</form >
<p id='confirmation'></p>
<input type='submit' id='vis' onClick='vis(); return false;' value='تایید اطلاعات'/>";
}
?>
You have not define the name in the submit button here:
<input type='submit' id='sb' value='ارسال اطلاعات' />
Replace above code with the below code:
<input type='submit' id='sb' name='email' value='ارسال اطلاعات' />
You are checking the code if(isset($_REQUEST['email'])) and there is no email field the form as I see.
Basically this check is used for verifying that form have submitted or not(as generally other do). So you need to add the name='email' in the submit button attribute to execute the if condition code..
You're setting a mime header but you're not setting any mime encoding, that might be one issue ;-)
Try using a dedicated package such as PEAR/Mail (and PEAR/Mail/Mime) or phpMailer.

How to print values of array using print_r php?

I have an array of values stored in variable "do"
It's something like this but not exactly
<html>
<input type='text' name='do' id='do'>
<input type='text' name='do' id='do'>
<input type='text' name='do' id='do'>
<input type='text' name='do' id='do'>
<input type='text' name='do' id='do'>
</html>
and I print it all with the use of [print_r] then it gives a result
<?php
print_r($_POST['do']);
//and i try this also
foreach($_POST as $key => $val){
echo $key . ' : ' . htmlentities($val,ENT_QUOTES) . "<br>\n";?>
}
[do=1&do=2&do=3]
how could I modify or just print the values like this:
1
2
3
Do not assign multiple elements the same ID (that's why it's out) but change the name in order to send an array of elements as POST
<input type='text' name='do[]'>
<input type='text' name='do[]'>
<input type='text' name='do[]'>
<input type='text' name='do[]'>
<input type='text' name='do[]'>
foreach($_POST as $key => $val)
echo $val;
^^ would do. However you cannot edit like $val=something. To change value, use $POST[$key]=something;
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
this won't work properly because you're overriding the same variable with different values and end up having one variable with the last value assigned.
Change this to
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
So you can loop through it like this:
foreach($_POST['do'] as $key => $val)
PHP overwrites the values with same plain names when parsing POST data into $_POST.
You may instead write the following form code:
<html>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
</html>
Now, $_POST['do'] is the array containing five do strings.
I think it can be done this way:
<input type="text" name="do[]" id="do_1" />
<input type="text" name="do[]" id="do_2" />
<input type="text" name="do[]" id="do_3" />
<input type="text" name="do[]" id="do_4" />
<input type="text" name="do[]" id="do_5" />
Then on POSTing, just look inside $_POST['do'] to get an array.
Edit: note that the apostrophes have been switched to quotes, the IDs have been made unique, and each tag has been self-closed. In general it will not validate otherwise.

Categories