PHP Form Post and Insert into multiple rows - php

I'm stumped. I've looked this up on multiple answers on Stackoverflow, but just can't get it. Maybe I'm just not seeing something.
I'm making a Family Feud game and using PHP and MySQL databases to store and retrieve information. For the Fast Money Round, I have a database with a Table called "FastMoney1" I'm using an HTML5 form and PHP to post the data in the form to that table, which has two columns: answer and score
I'm running my query through a for loop, but it's not posting anything to the table. I'm wondering what I'm doing wrong.
HTML:
<form method="post" class="form-horizontal">
<div class="form-group">
<div class="col-xs-9">
<input type="text" class="form-control" id="question1answer" name="answer[0]" placeholder="Question 1">
</div>
<div class="col-xs-3">
<input type="number" class="form-control" id="question1score" name="score[0]" placeholder="0">
</div>
</div>
<div class="form-group">
<div class="col-xs-9">
<input type="text" class="form-control" id="question1answer" name="answer[1]" placeholder="Question 2">
</div>
<div class="col-xs-3">
<input type="number" class="form-control" id="question1score" name="score[1]" placeholder="0">
</div>
</div>
<div class="col-xs-4 col-xs-offset-4" align="center">
<input type="submit" class="btn btn-success" name="Submit" />
</div>
</form>
PHP:
<?php
if(isset($_POST['submit'])){
require "config.php";
for ($i = 0; $i<count($_POST); $i++){
$answer = $_POST['answer'][$i];
$score = $_POST['score'][$i];
$sql = "INSERT INTO `fastMoney1`(`answer`, `score`) VALUES ('$answer','$score')";
if ($conn->query($sql) === TRUE) {
echo "";
} else {
echo $conn->error;
}
}
$conn->close();
echo "<meta http-equiv='refresh' content='0'>";
}
?>
All of this lives on the same PHP page, which is why I do not have an action attached to the form.
The config.php is an include that calls the host, username, password and database and opens the connection

Remember that PHP variables as case sensitive you have given name Submit in form while in php you are checking if(isset($_POST['submit'])){ which never become true.
change it to
if(isset($_POST['Submit'])){ //<----- S in upper case
EDIT
You also need to change your loop to
for ($i = 0; $i<count($_POST['answer']); $i++){

see your sql statement you don't need those ampersand in table name and column names INSERT INTO fastMoney1(answer, score) VALUES ('$answer','$score')

<input type="submit" class="btn btn-success" name="submit" />
i changed
name="Submit"
to
name="submit"
"Submit" to "submit" ----->"S" to "s"
and it works fine

Related

How to post an current id

I am making a forum, and I can add a reaction to save in the database, the only problem now is that it will leave empty id in the database.
The database:
How it will post it now:
Now I need to find a way to add the ledenpagina_id, topic_id and klant_id automatic. The klant_id needs to be set based on the klant session, but I am not sure how to get that.
The topic_id is set as active_id like this:
$actieftopicid = $topic['id'];`
But I don't know how to add that in the post so it will save it the correct way in the database.
I have tried doing this:
$q1['topic_id'] = $app->check_string($_POST[$topic['id']]);
But that isn't working.
The code to post it in the database:
<?php
if(isset($_POST['react_btn'])){
unset($q1);
$q1['reactie'] = $app->check_string($_POST['reactie']);
$q1['topic_id'] = $app->check_string($_POST[$topic['id']]);
$app->insert_query('reacties', $q1, 'id');
}
?>
<form action="" method="post">
<div class="form-group">
<label for="comment">Reactie:</label>
<textarea class="form-control" name="reactie" rows="3" id="comment"></textarea>
<button type="submit" name="react_btn" class="btn btn-primary">Plaats reactie</button>
</div>
</form>
You can add the id of the topic into the form like this:
<form action="" method="post">
<div class="form-group">
<label for="comment">Reactie:</label>
<textarea class="form-control" name="reactie" rows="3" id="comment"></textarea>
<input type="hidden" name="topicid" value="<?php echo $topic['id']; ?>">
<button type="submit" name="react_btn" class="btn btn-primary">Plaats reactie</button>
</div>
</form>
and then you can use it like $_POST['topicid'], since that is the name of the hidden input. Also, instead of
unset($q1);
you need to initialize $q1 properly:
$q1 = array();

How exactly do I make a button start a database (sql) query in php?

I'm trying to create a feedback page on this website I'm coding and I need my submit button to start a query so that the message is sent and stored in a feedback table in my database.
But I can't seem to be able to find the reason my implementation does not work. I think it worked this way the last time, any help would be appreciated.
<div class="form-group">
<label for="msgType">What is your concern?</label>
<select name="msgType" method="post" class="form-control input-lg" id="msgType">
<option value="1">I wish to report incorrect information cited on the website.</option>
<option value="2">I wish to report the existence of another bistro.</option>
<option value="3">I wish to thank the admin personally.</option>
<option value="4">I wish to express some other concern.</option>
</select>
</div>
<div class="form-group">
<label for="message">Your message:</label>
<textarea class="form-control" method="post" rows="15" id="message" name="message" placeholder="Thou, dear customer, may express Your concern here in any extent or form your majesty finds appropriate."></textarea>
</div>
<button type="submit" class="btn btn-info">Send</button>
<input type="hidden" name="submitted_feedback" value="1">
</div>
</section>
</div>
</div>
</section>
</body>
</html>
<!-- QUERY SECTION -->
<?php
if(isset($_POST['submitted_feedback']) == 1)
{
$message = mysqli_real_escape_string($dbc, $_POST['message']);
$q = "INSERT INTO feedback(type, message) VALUES ($_POST[msgType], '$message')";
$r = mysqli_query($dbc, $q);
echo 'dis is running';
if($r)
{
echo "<script type='text/javascript'>alert('Your message was sent! :)');</script>";
}
}
?>
Buttons with the type submit, are intended to submit forms. Your markup doesn't have any <form> </form> tags at all.
Here you go

input field not updating to sql

Can anyone help me out, not the expert on php/sql. Have looked about but can't find my answer to my problem. My logged in user has an inout field which they put in their answer. On the submit button I want it to update to the sql the users answer. But nothing happens
I have an input field which is this.
<div class="collapse navbar-collapse">
<form class="navbar-form navbar-right" method="post">
<div class="form-group">
<input type="text" name="answer" placeholder="Answer" class="form-control" value="<?php ($_POST['answer']); ?>" />
<input type="submit" name="submit" class="btn btn-success" value="Submit" />
</div>
</form>
And my php which is this.
function post_answer() {
$answer = $_POST['answer'];
$sql = "UPDATE users SET answer = '".$answer."' WHERE id='".$_SESSION['id']."'";
}
Any help would be much appreciated.

How do I use HTML & PHP forms to insert data into a database?

So I am actually not sure what I am doing wrong, and I am sure it is just some small mistake, but I can't seem to get the submit button to take the information in the form and and send it to the database.
Here is what my code looks like.
So with this I am able to successfully connect to the database:
<?php
$mysql_host = 'localhost';
$mysql_pass = '';
$mysql_user = 'root';``
$mysql_db = 'blog';
if (! #mysql_connect($mysql_host, $mysql_user, $mysql_pass) || ! #mysql_select_db($mysql_db)){
die('Error');
}
?>
and I able to see the contents of table by printing them to the screen.
I believe the problem is here in this simple html form:
<form action="index.php" method = "post">
<div class="ui form">
<div class="field" name = "title">
<label>Title</label>
<input type="text">
</div>
<div class="field" name = "post">
<label>New Post</label>
<textarea></textarea>
</div>
</div>
<br>
<div class="ui submit button" name = "submit">Submit New Post</div>
</form>
I am just curious as to which php code I should use to connect this form so I am able to send the contents of the form over to the database.
elements can be styled just like elements and can have type="submit" so the instead of div you can use and avoid extra spaces between attributes and values. Instead of your code:
<div class="ui submit button" type="submit" name="submit">Submit New Post</div>
you can use:
<button class="ui submit button" type="submit" name="submit">Submit New Post</button>
as like :
<form action="index.php" method="post">
<div class="ui form">
<div class="field">
<label>Title</label>
<input type="text" name="title">
</div>
<div class="field">
<label>New Post</label>
<textarea type="text" name="post"></textarea>
</div>
</div>
<br>
<button class="ui submit button" type="submit" name="submit">Submit New Post</button>
</form>
I hope you will get your desire result.
provide the name field into the html tags e.g.
<form action="index.php" method="post">
<input type="text" name="title">
<input type="text" name="Sub-title">
<input type="submit" name="submit">
</form>
Inside index.php you can write
$title = $_POST['title'];
$subtitle = $_POST['Sub-title'];
You missing name attribute from the fields and elements can be styled just like elements and can have type="submit" so the instead of div you can use and avoid extra spaces between attributes and values.
and change input of a submit div
Submit New Post
You are missing name attribute from the input fields , and the submit should be a input if you are a using normal PHP form submit
<input type="text" name="title">
and change the submit div to a input
<input type="submit" name="submit" value="Submit New Post" />
You are missing a name attribute in your form fields:
<input type="text">
Should be something like:
<input type="text" name="title">
This is mandatory in order to be able to retrieve your POST datas in your PHP page, and it applies to every input you need to process in your PHP page (in this case your <input> and your <textarea> elements).
Once done this, you'll be able to retrieve each specific POST value in your PHP page by accessing the superglobal array $_POST:
$title = $_POST['title'];
The problem is here:
<div class="ui submit button" name = "submit">Submit New Post</div>
it should be:
<div class="ui submit button" type="submit" name = "submit">Submit New Post</div>
However, I suggest you use PDo to handle this for better.

Inserting multiple rows to mysql fetched from while loop

I am new to stackoverflow and little bit confused about what to title this question, but let me explain what my problem is.
I am developing survey system where I have option for admin to enter the questions and answers from the back end, so in the front end I am displaying the question, answers using the while loop in the form.
My problem is how to write the code to enter the multiple values (survey filled by the customer) in the mysql. I am attaching my code here.
$(function() {
$('form').bind('submit', function(){
$.ajax({
type: 'post',
url: "/prs/pageCode.php",
data: $("form").serialize(),
success: function() {
alert("Data Saved, Press Next!");
}
});
return false;
});
});
form is below
<form name="form" id="form" class="form-horizontal" action="#"
method="POST">
<fieldset>
<div class="control-group">
<label class="control-label" for="focusedInput">Name</label>
<div class="controls">
<input name="pname" class="input-xlarge focused" id="focusedInput"
type="text" value="" required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Email</label>
<div class="controls">
<input name="pemail" class="input-xlarge focused" id="focusedInput"
type="text" value="" required>
</div>
</div>
<div class="control-group">
<label>We would like to post your comments to internet rating sites,
while we may use your name or an alias name. Please select your
desire:</label>
</div>
<div class="control-group">
<label class="control-label" for="focusedInput"></label>
<div class="controls">
<p>
<label> <input type="radio" name="choice" value="yes" id="choice_0">
Yes, use my name as entered above
</label> <label> <input type="radio" name="choice" value="no"
id="choice_1" onChange="enabletextbox();"> Use the alias name, as
I enter below
</label> <input id="alias" name="alias" type="text"
class="input-xlarge focused">
</p>
</div>
</div>
</fieldset>
</div>
<div class="tab-pane" id="tab2">
<fieldset>
<?php
$i=1;
while($row=mysqli_fetch_array($questions)){
?>
<div class="control-group">
<label class="control-label" for="focusedInput">(<?php echo $i;?>) <?php
$questionid = $row['question_id'];
$question = $row['question'];
echo $row['question']; ?></label>
<div class="controls">
<?php
if($row['answer_type']=="Ratings") {
echo "
<p>
Low<input type='radio' name='rating$i' value='1' id='rating_0'>
<input type='radio' name='rating$i' value='2' id='rating_1'>
<input type='radio' name='rating$i' value='3' id='rating_2'>
<input type='radio' name='rating$i' value='4' id='rating_3'>
<input type='radio' name='rating$i' value='5' id='rating_4'>High
</p>
";
}
else if ($row['answer_type']=="Comments") {
echo "<textarea name='answer' cols='' rows=''></textarea>";
}
$i++;
echo "<br />";
$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) values (1,$surveyid,$questionid,'$question','$rating$i','$_POST[answer]')";
$result2 = mysqli_query($con,$query2);
if(!$result2) {
echo mysqli_error($result2);
}
?>
</div>
</div>
<?php }?>
</fieldset>
</div>
<div class="tab-pane" id="tab3">
<fieldset>
<div class="control-group">
<label class="control-label" for="focusedInput">Testimonial about
your care by <?php echo $_SESSION['doctorname'];?>
</label>
<div class="controls">
<textarea name="review" cols="5" rows="5" required></textarea>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn btn-primary"
value="Save & Press Finish"></input>
</div>
</fieldset>
pageCode.php
<?php
session_start();
require_once('config.php');
$con=mysqli_connect(HOST,USER,PASSWORD,DATABASE);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$alias = $_POST['choice'];
if($alias=="yes")
$aliasname = $_POST['pname'];
else
$aliasname = $_POST['alias'];
$sql="INSERT INTO reviews (doctor_id,doctor_name,survey_id,name,email,display_name,overall_review)
VALUES
('$_SESSION[doctorid]','$_SESSION[doctorname]',$_SESSION[surveyid],'$_POST[pname]','$_POST[pemail]','$aliasname','$_POST[review]')";
$result = mysqli_query($con,$sql);
//$result2 = mysqli_query($con,$_SESSION['insertallquery']);
if(!$result) {
echo mysqli_error($result);
}
?>
I actually want to insert the answers into database filled by customers.
You need to store answers in the review table or a separate table its your wish.
but in general, what I understood you have a separate table for questions with the question ids.
So, you need to add column in the review table or a separate table with the question ids as the field names.
and store the respective answers in those column for that user.
For eg. considering adding column in existing reviews table :
INSERT INTO reviews (doctor_id,doctor_name,survey_id,name,email,display_name,overall_review,q1,q2,q3...)
so on according to number of questions you have.
Also, when you add a new question to the questions table you will have to make sure to add a column in this table using ALTER TABLE with the.
For getting the answers as the question are in a while loop, create a array.
and insert those values into mysql using a while loop iterating the array.
The query inside the while loop will be like this :
INSERT INTO reviews (q.$i) VALUES ($answer[$i]) WHERE docter_id='$_SESSION[doctorid]'
You could get the values from IDs/class names or tag names itself then you could store it in an array then you can pass it to your jquery code( $.ajax ) or ($.post) then accept the array variable declared in your javascript
but our tags must contain temp index values example.
Another way is to pass the answers as JSONs then you could use json_encode in php to access the answers

Categories