how to post textarea value into database - php

I'm having trouble with posting values typed into textarea. everything else works well, any idea how to make it work?
HTML:
<form id="formData2" action="artistuploader.php" method="post"
enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000"></input>
<br/>
<input id="inputField" type="text" name="actname" placeholder="Act Name" >
<br>
<input id="inputField" type="text" name="fullname" placeholder="Full Name" >
<br>
<input id="inputField" type="text" name="genre" placeholder="Genre" >
<br>
<textarea id="inputField" name="biography" form="formData2" placeholder="Biography"<?php echo $biography; ?>></textarea>
<br>
<input id="inputField" type="file" name="artistImage" placeholder="Artwork" >
<br>
<input id="inputField" type="text" name="imagepath" placeholder="Image path URL" >
<br>
<input id="submitButton" type="submit" name="uploadArtist" value="Register Artist">
</form>
PHP
<?php
$msg = "";
//if Upload button is pressed
if (isset($_POST['uploadArtist'])){
$target = "uploads/artistPics".basename($_FILES['artistImage']['name']);
//connecting to our database
$db = mysqli_connect("127.0.0.1", "user", "pass", "tablename");
$tmp_name = $_FILES['artistImage']['tmp_name'];
$name = $_FILES['artistImage']['name'];
//getting the submitted form data
$ActName = $_POST['actname'];
$FullName = $_POST['fullname'];
$Genre = $_POST['genre'];
$ArtistPhoto = $_FILES['artistImage']['name'];
$imageURLpath = $_POST['imagepath'];
$Biography = $_POST['biography'];//having problem with this line here
//saving submitted data into database table songsDB
$sql = "INSERT INTO artistsdb (ActName,FullName,Genre,ArtistPhoto,Biography,imageURLpath) VALUES ('$ActName','$FullName','$Genre','$ArtistPhoto','$Biography','$imageURLpath')";
mysqli_query($db, $sql); //stores the submitted data into table
//now moving the uploaded image to uploads folder
if(move_uploaded_file($_FILES['artistImage']['tmp_name'], $target)){
$msg = "Uploaded Successful";
}else{
$msg = "There was a problem uploading Data";
}
}
//header("refresh:1; url=index.php"); ?>

Replace your textarea with
<textarea id="inputField" name="biography" form="formData2" placeholder="Biography"><?php echo $biography; ?></textarea>

Related

How to retrieve and confirm form inputs on another php file

I have The following form inputs I am trying to send these input data to "placebet.php" then retrieve the data and add a confirm or cancel button, then It can add to the database
<form action="placebet.php" method="post">
<div id="box" class="boxlit">
<div class="box" data-id="0">Myanmar - Vietnam<br>Home [1]<div class="crtTotal">4.30</div>
<input type="hidden" name="kickoff[]" value="7/17/2022 10:00">
<input type="hidden" name="match[]" value="Myanmar - Vietnam">
<input type="hidden" name="result[]" value="Home [1]" readonly="">
<input type="hidden" name="value[]" value="4.30"></div>
<div class="box" data-id="4">Thailand - Philippines<br>Draw [2]<div class="crtTotal">3.20</div>
<input type="hidden" name="kickoff[]" value="7/17/2022 13:30">
<input type="hidden" name="match[]" value="Thailand - Philippines">
<input type="hidden" name="result[]" value="Draw [2]" readonly="">
<input type="hidden" name="value[]" value="3.20"></div>
<div class="box" data-id="11">Botswana - Cameroon<br>Away [3]<div class="crtTotal">1.35</div>
<input type="hidden" name="kickoff[]" value="7/17/2022 22:00">
<input type="hidden" name="match[]" value="Botswana - Cameroon">
<input type="hidden" name="result[]" value="Away [3]" readonly="">
<input type="hidden" name="value[]" value="1.35"></div></div><br>
<input type="hidden" name="account[]" value="0818054386" readonly="">
<input type="hidden" name="balance[]" value="20" readonly="">
<input type="hidden" id="todds" name="todds[]" value="18.58" readonly="">
<input type="hidden" id="inp" name="payout[]" value="92.90" readonly="">
<div>Total Odds: <b id="ct1">18.58</b></div><br>
<div>(N$)Stake: <input id="stake" type="number" name="stake[]" value="5"> NAD</div><br>
<div>Payout: N$ <b id="payout">92.90</b></div>
<input class="bet1" type="submit" name="submit" value="Bet">
</form>
Php code in "placebet.php"
I'm not sure if the code below is correct but I need it to show the input data from the form and give me a option to confirm the data(button) and then it can finally add to the database
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "forms");
$dba = mysqli_connect("localhost","root","","login");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$error = false; //set the error status value
$error_msg = "";
$back = mysqli_real_escape_string($link, $_REQUEST['kickoff'][0]);
$total = count($back); // get the length of the match
for($i=0;$i<$total;$i++){
// Escape user inputs for security
$kickoff = mysqli_real_escape_string($link, $_REQUEST['kickoff'][$i]);
$match = mysqli_real_escape_string($link, $_REQUEST['match'][$i]);
$selection = mysqli_real_escape_string($link, $_REQUEST['result'][$i]);
$odd = mysqli_real_escape_string($link, $_REQUEST['value'][$i]);
$account = mysqli_real_escape_string($link, $_REQUEST['account'][0]);
$stake = mysqli_real_escape_string($link, $_REQUEST['stake'][0]);
$payout = mysqli_real_escape_string($link, $_REQUEST['payout'][0]);
$todds = mysqli_real_escape_string($link, $_REQUEST['todds'][0]);
$accabal = mysqli_real_escape_string($link, $_REQUEST['balance'][0]);
//run sql query for every iteration
$charge = mysqli_query($dba, "UPDATE users SET balance = $accabal- $stake WHERE username='".$_SESSION['username']."'") ;
$_SESSION["balance"] = $accabal- $stake ;
$date = date ('Ymd');
$create = mysqli_query($link,"CREATE TABLE R$date LIKE receipts") ;
$insert = mysqli_query($link,"INSERT INTO `R$date`(`Match`, `Selection`, `Odd`,`Account`,`Stake Amount`,`Payout`,`Total Odds`) VALUES ('$match','$selection','$odd','$account','$stake','$payout','$todds')");
if(!$insert)
{
$error = true;
$error_msg = $error_msg.mysqli_error($link);
}
//check your error status variable and show your output msg accordingly.
if($error){
echo "Error :".$error_msg;
}else{
header("location: index.php");
exit;
}
}
mysqli_close($db);
?>
What you want to do isn't redirect to index.php, cause with this you start a new request and cant point on the request data of placebet.php anymore.
You want either to send your form via javascript ajax request and then react to the response of placebet.php (https://www.w3schools.com/js/js_ajax_intro.asp) or generating your own new output at placebet.php which then can be a confirm page or something similar.
e.g.
if($error){
echo "Error :".$error_msg;
}else{
echo "Data has been stored!";
}
You also could put your html at the end of the php file after closing the php part with ?> like mentioned here https://www.thoughtco.com/php-with-html-2693952#:~:text=As%20you%20can%20see%2C%20you,re%20inside%20the%20PHP%20tags).

How to insert images and data into mysql using php?

I am having a trouble to saving data into the database. My connection details and sql insert query everything is correct and image is also uploading to folder but I do not know why data along with image is not saving into an database when i hit upload button.Can anyone help me please?
My php code
<?php
include('server.php');
$userID = 1;
if(isset($_SESSION['username']))
{
$userName = $_SESSION['username'];
$queryID = "SELECT id from users WHERE username = '$userName'";
$resultID = $db->query($queryID);
$row=$resultID->fetch_assoc();
$userID = $row['id'];
}
if(isset($_POST['submit']))
{
$image = $_FILES['image']['name'];
$target = "images/".basename($image);
$eventName = $_POST['eventName'];
$eventDetail = $_POST['eventDetail'];
$eventDate = $_POST['eventDate'];
$eventTime = $_POST['eventTime'];
$queryImage = "INSERT INTO event_detail(eventName,eventDetails,eventDate,eventTime,imagePath,userID) VALUES('$eventName','$eventDetail','$eventDate','$eventTime','$image','$userID')";
mysqli_query($db,$queryImage);
if(move_uploaded_file($_FILES['image']['tmp_name'],$target))
{
$msg = "Image uploaded successfully";
}
else
{
$msg = "There is problem";
}
}
?>
html
<form method="post" enctype="multipart/form-data">
<label for="eventName">Event Name:<label>
<input type="text" id="eventName" name="eventName" ><br><br>
<label for="eventDetail">Event Detail:<label>
<textarea id="eventDetail" name="eventDetail" ></textarea><br><br>
<label for="eventDate">Event Date:<label>
<input type="text" id="eventDate" name="eventDate" ><br><br>
<label for="eventTime">Event Time:<label>
<input type="text" id="eventTime" name="eventTime" ><br><br>
<input type="file" id="image" name="image"><br><br>
<button type="submit" id="submit" name="submit" >Submit</button>
</form>
Change this
$queryImage = "INSERT INTO event_detail(eventName,eventDetails,eventDate,eventTime,imagePath,userID) VALUES ('$eventName','$eventDetail','$eventDate','$eventTime','$image','$userID')";
to
$queryImage = "INSERT INTO event_detail(eventName,eventDetails,eventDate,eventTime,imagePath,userID) VALUES ($eventName,$eventDetail,$eventDate,$eventTime,$image,$userID)";

Why is my form not working using POST method?

I have written the below HTML code:
<form action="index.php" method="POST">
<input type="text" name="title" required>
<input type="text" name="brief_text" required>
<textarea name="text" required></textarea>
<input type="submit" name="add" value="Add">
</form>
My PHP code:
<?php
require_once('db.php');
if(isset($_POST['add'])){
$title = $_POST['title'];
$brief_text = $_POST['brief_text'];
$text = $_POST['text'];
$blog_cat_id = $_POST['blog_cat_id'];
if($title AND $brief_text AND $text AND $blog_cat_id){
$insert_blog = "insert into blog values ('','$title','$brief_text','$text','$blog_cat_id',NOW())";
$run_insertion = mysqli_query($con, $insert_blog);
if($run_insertion){
echo "Blog has been added!";
}
else{
echo "Error adding blog!!!";
}
}
else{
echo "All fields are required!";
}
}
else{
echo "GOODBYE";
}
?>
Every time I refresh the page, it only shows the form and "GOODBYE" and does not even insert the data into database table.
Help me out please.
Is it still showing 'GOODBYE' now you've changed
$_POST['add_blog']
to
$_POST['add']
when you click submit?
You have few mistakes,
1) should be: $_POST['add'] instead of $_POST['add_blog']
2) don't have $_POST['blog_cat_id'] as not in form
EDIT
Copied your code and made some changes:
code:
if(isset($_POST['add'])){
print_r($_POST);
$title = $_POST['title'];
$brief_text = $_POST['brief_text'];
$text = $_POST['text'];
$blog_cat_id = $_POST['blog_cat_id'];
if($title AND $brief_text AND $text AND $blog_cat_id){
echo "inside condition";
$insert_blog = "insert into blog values ('','$title','$brief_text','$text','$blog_cat_id',NOW())";
$run_insertion = mysqli_query($con, $insert_blog);
if($run_insertion){
echo "Blog has been added!";
}
else{
echo "Error adding blog!!!";
}
}
else{
echo "All fields are required!";
}
}
else{
echo "GOODBYE";
}
HTML:
<form action="index.php" method="POST">
<input type="text" name="title" required>
<input type="text" name="brief_text" required>
<input type="text" name="blog_cat_id" required>
<textarea name="text" required></textarea>
<input type="submit" name="add" value="Add">
</form>
output
Array ( [title] => test [brief_text] => test [blog_cat_id] => 1 [text] => testing [add] => Add )
inside condition
Now, check your query if doesn't work.
Hope this will help you.
Your query is wrong, columns are not specified and you are open to sql injection you should learn to use parameterized query. but for this time you can use the following.
Try this:
htmlcode
<form action="index.php" method="POST">
<input type="text" name="title" required>
<input type="text" name="brief_text" required>
<input type="text" name="blog_cat_id" required>
<textarea name="text" required></textarea>
<input type="submit" name="add" value="Add">
</form>
index.php
<?php
require_once('db.php');
if(isset($_POST['add'])){
$title = $_POST['title'];
$brief_text = $_POST['brief_text'];
$text = $_POST['text'];
$blog_cat_id = $_POST['blog_cat_id'];
if($title AND $brief_text AND $text AND $blog_cat_id){
$insert_blog = "insert into blog('col1','col2','col3','col4','col5','col6') values ('','$title','$brief_text','$text','$blog_cat_id',NOW())";
$run_insertion = mysqli_query($con, $insert_blog);
if($run_insertion){
echo "Blog has been added!";
}
else{
echo "Error adding blog!!!";
}
}
else{
echo "All fields are required!";
}
}
?>
Note : col1, col2, col3, col4, col5 and col6 will be your column name.

could not inserting data to mysql table using php

I have following form in html
<form method="post" enctype="multipart/form-data" />
<fieldset>
<legend>Activate Scheme</legend>
<p>Date Of Draw</p>
<p><input type="text" name="dateOfDraw" class="textBox" style="width:150px" /></p>
<p>Time Of Draw</p>
<p><input type="text" class="textBox" name="timeOfDraw" style="width:150px" /></p>
<p>Enter scheme name</p>
<p><input type="text" class="textBox" name="schemeName" style="width:150px" />
</p>
<p>Upload Image</p>
<p><input type="file" name="image" id="image" /></p>
<p><input name="scheme_button" type="submit" class="button1" value="Submit"></p>
</fieldset>
</form>
Problem is that their is no error when I execute the following query
<?php
if(isset($_POST['submit_button']) && count($_POST)>0) {
print_r($_POST);
$dateOfDraw = $_POST['dateOfDraw'];
$timeOfDraw = $_POST['timeOfDraw'];
$schemeName = $_POST['schemeName'];
$imageName = $_FILES['image']['name'];
$destination = '../images/'.$imageName;
$source = $_FILES['image']['tmp_name'];
if(move_uploaded_file($source, $destination)) {
echo 'file uploaded';
} else {
echo ' file not uploaded';
}
$sSQL = "INSERT INTO landing_page(dateOfDraw, timeOfDraw, schemeName, image) VALUES('$dateOfDraw','$timeOfDraw','$schemeName','$imageName')";
echo $sSQL;
if(!$sSQL){
die(mysqli_query($con));
}
mysqli_query($con,$sSQL);
//header("location: list_products.php");
}
?>
but when I check my data in a mysql database the rows are still empty. Please check it am I missing something or the php code is wrong.
Note: I am using php version 5.5.16
The name of your submit button is scheme_button and that's exactly the thing that you need to $_POST.
if(isset($_POST['scheme_button']) && count($_POST) > 0)
You form doesn't have an action and it doesn't know where to go. So add one.
<form method="post" action="index.php" enctype="multipart/form-data" />
firstly you have to name of submit button
if(isset($_POST['scheme_button']) && count($_POST) > 0)
secondly if insert code in same page then
if insert code in another page then
action = "filename"

how to store multiple array of same name from one form

suppose i m trying to store name,phone email of three person from one form here is my code..
<form method="post" action="demo.php">
<input type="text" name="name[]">
<input type="text" name="phone[]">
<input type="text" name="email[]">
<br>
<input type="text" name="name[]">
<input type="text" name="phone[]">
<input type="text" name="email[]">
<br>
<input type="text" name="name[]">
<input type="text" name="phone[]">
<input type="text" name="email[]">
<br>
<input type="submit">
</form>
now code of demo.php which is my action page...
foreach(($_POST['name']as $id)
{
$name= mysql_real_escape_string($id);
$query1 = "INSERT INTO list (name,phone,email) VALUES ('$name','$_POST[phone]',$_POST[email])";
$query = mysql_query($query1);
}
if($query)
{
return true;
}
else{
echo "Something Wrong";
}
its storing name of three people correctly but not storing phone and email of three people
i tried with for loop also but not getting result,plz anyone tell me how to store multiple array from a single form.
You can do this:
// variables of the form
$phone = $_POST['phone'];
$email = $_POST['email'];
foreach($_POST['name']as $k => $id)
{
$name= mysql_real_escape_string($id);
$query1 = "INSERT INTO list (name,phone,email) VALUES ('$name','$phone[$k]','$email[$k]')";
$query = mysql_query($query1);
}
if($query)
{
return true;
}
else{
echo "Something Wrong";
}

Categories