could not inserting data to mysql table using php - 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"

Related

how to post textarea value into database

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>

Trying to pass variable values from PHP to a new HTML file

using PHP 5.6.27 I'm trying to generate a HTML file by combining
(1) Input from form fields
(2) HTML code (template)
Issue: The file gets created but only has the $template value but not the form field input.
Code is below. Also Live link is at http://fastlaw.in/formv2.html
Sure I'm missing something simple, but would appreciate it if anyone can help out.
formv2.html
<form action="formscriptv2.php" method="POST">
<input id="field1" type="text"/> <br />
<input id="field2" type="text" /> <br />
<input type="submit" name="submit" value="Save Data">
</form>
formscriptv2.php
<?php
$Field1 = $_POST['field1'];
$Field2 = $_POST['field2'];
$onepart = "
<div class='table table-left'>
<section>
<h2 class='table-heading'>";
$twopart = "</h2>
<table class='data'>
<caption>
<p>";
$threepart = "</p>
</caption>
";
$template = $onepart . $Field1 . $twopart . $Field2 . $threepart;
$ret = file_put_contents('tmp/mydata.txt', $template, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
?>
PHP $_POST array are populated via the name HTML attribute:
<input id="field1" name="field1" type="text"/> <br />
<input id="field2" name="field2" type="text" /> <br />

Undefined Index in vars

I'm getting this error in my variables on lines 26 and 27. I have been searching for the problem itself, some people say that the variables are not initialized. Althought I think they are. Also I saw people saying to use isset() / !empty() but I don't understand that, and what it does.
<?php
$nome = $_POST['nome']; //26
$preco = $_POST['preco']; //27
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "crc", "root");
mysql_select_db ("crc");
$imgData =addslashes(file_get_contents($_FILES['userImage'['tmp_name']));
$sql = "INSERT INTO fios (nome,preco,imagem)VALUES('$nome','$preco','{$imgData}')";
$current_id = mysql_query($sql) or die("<b>Erro:</b> Problema na imagem inserida!<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: veradmin.php");
}}}
?>
<!DOCTYPE html>
<html>
<title>Inserir</title>
</head>
<body>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<div align="center">
</p><tr>
<td width="321"><strong>Nome/Descricao:</strong></td>
<td width="102" align="left">
<input type="text" name="nome" value="" size="40" />
</td>
</tr><p>
</p><tr>
<td width="321"><strong>Preco:</strong></td>
<td width="102" align="left">
<input type="text" maxlength="9" name="preco" value="" size="20" />
</td><p>
</p></tr>
<input name="userImage" type="file" class="inputFile" /><p>
</p><input type="submit" value="Inserir Registo" class="btnSubmit" />
</form>
</div>
</body>
</html>
This issue happened because in the first load $_POST['nome'] and $_POST['preco'] is empty and these indexes does not exists.
In this cases you should check with !empty to run this lines:
if(!empty($_POST['nome']) && !empty($_POST['preco']))
{
if(count($_FILES) > 0) {
...
}
}
At this time if you post your form or not these codes will run which is root cause of showing these notices

PHP - Processing after input is entered twice in a row

I have a problem with my form.
I have an HTML Form as follows:
<form action="index.php" method="post">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Now my problem is:
Every time I process this form for the first time, it should output an error. The error has to be shown every time, if this is the first try.
If I enter the same input value twice, it should succeed.
I thought about setting a session variable $_SESSION['first_visit] if I'm processing the form for the first time.
I also thought about saving the $_POST['text'] - Value into Session but it's being overwritten every time.
Thank you for your answers.
<?php
extract($_POST);
if( isset($send) )
{
if( $fired <= 0 )
{
$fired++;
echo "Nope, error. send again to succeed";
}
else
{
echo "Yay success";
}
}
?>
<form action="" method="post">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Here's some quick and dirty snippet. We set a hidden input field with an initial value of 0 and count it up everytime the form has been send. The only thing to do afterwards is to check if the value of the hidden field is bigger than 0
You can use cookies instead of session
<form action="index.php" method="post" id="exampleForm">
<input type="text" name="text" id="text">
<input type="button" value="Send" name="send" onClick="checkVisit()">
</form>
<script>
function checkVisit(){
var isFirstVisit = getCookie("first_visit");
if(isFirstVisit == "" || isFirstVisit == 1){
setCookie("first_visit",0,1);
}else if (isFirstVisit == 0){
setCookie("first_visit",1,1);
document.getElementById("myForm").submit();
}
}
</script>
Thanks for this fast Answer! Your Tipp helped me with my problem a lot.
Here is the working Script:
<?php
session_start();
if(isset($_POST['go'])) {
$text = $_POST['text'];
$fired = $_POST['fired'];
if($fired <= 0) {
$fired++;
echo "Nope";
$_SESSION['val'] = $_POST['text'];
}
else {
if($fired > 0 && $_SESSION['val'] == $_POST['text'] ) {
echo "Success";
}
else {
echo "Failed";
$_SESSION['val'] = $_POST['text'];
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="test2.php" method="post">
<input type="text" name="text" id="text">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="hidden" name="val" id="val" value="">
<input type="submit" value="Senden" name="go">
</form>
</body>
</html>

PHP - editing data in db issue

I'm going to keep it short and simple. I'm writing a really basic code in php which adds content to mysql db and I've run into an issue with editing. This is my form:
if($idExists)
{
Print '
<form action="editIt.php" method="POST">
<div name="id"> '. $id . '</div>
Enter new detail: <input type="text" name="details"/><br/>
public post? <input type="checkbox" name="public[]" value="yes"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}
And this is my editIt.php
//session start and stuff
if(filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING) == "POST")
{
echo "<script type='text/javascript'>alert('EDITIT!');</script>";
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("WebSiteDB") or die ("Cannot connect to database");
$id = $_POST['id'];
$details = mysql_real_escape_string($_POST['details']);
$time = strftime("%X");
$date = strftime("%B %d, %Y");
$isPublic = 'no';
foreach($_POST['public'] as $eachCheck)
{
if($eachCheck != NULL)
$isPublic = "yes";
}
mysql_query("UPDATE list SET details='$details', dateEdited='$date', timeEdited= '$time', public='$isPublic' WHERE id='$id'");
header("location: home.php");
}
I can't really find an issue with this code (which is not really strange, I'm a newbie at web stuff) and yet it just goes to home.php and does not change data in DB. Please, help me jump this ledge, so I can go on with my life.
I think, the problem is in this line $id = $_POST['id'];. On form submit, the input field value will only be submitted, not the DIV value.
So, please change from :
if($idExists)
{
Print '
<form action="editIt.php" method="POST">
<div name="id"> '. $id . '</div>
Enter new detail: <input type="text" name="details"/><br/>
public post? <input type="checkbox" name="public[]" value="yes"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}
To :
if($idExists)
{
Print '
<form action="editIt.php" method="POST">
<input type="hidden" name="id" value="' . $id . '">
Enter new detail: <input type="text" name="details"/><br/>
public post? <input type="checkbox" name="public[]" value="yes"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}

Categories