HTML Form calling PHP returns blank - php

My html form is:
<form enctype="multipart/form-data" action="AddEmail.php" method="GET>
<table>
<tr>
<td>Email Address</td>
<td>First Name</td>
<td>Surname</td>
<td><td>
</tr>
<tr>
<td> <input type="text" name="emailAddress" value="example#email.com"> </td>
<td> <input type="text" name="firstName" value="First Name"> </td>
<td> <input type="text" name="surname" value="Surname"> </td>
<td> <input type="submit"> </td>
</tr>
</table>
</form>
and the php that it is calling is:
$Email = $_GET["email"]
$firstName = $_GET["fName"]
$surName = $_GET["surname"]
$sql = "INSERT INTO emailaddress (EmailAddress, FirstName, LastName)
VALUES ($Email, $firstName, $surName)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>`
It is meant to pass the inputted information on to a database. It is connecting to the database without an issue when I use already use pre-created values for it to input, but as soon as I try and get it to use inputted values and call the php it just returns with a blank page.

You are missing the semicolons at the end of the line for the first 3 lines of the php file.
A blank page is typical for a syntax error. Whenever a blank page is displayed for no good reason put the following lines at the top of your php file and you should see the php error displayed.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Besides that:
You will also receive a SQL error because you are not enclosing the php variables in quotes when you construct the query. You should REALLY consider escaping all user submitted data.
$_GET["email"] should be $_GET["emailAddress"] as set in the form
$_GET["fName"] should be $_GET["firstName"] as set in the form
Consider using the html attribute "placeholder" instead of writing the hint as a value.
Also, what user3590911 said about the quote

Your
<form enctype="multipart/form-data" action="AddEmail.php" method="GET>
should be:
<form enctype="multipart/form-data" action="AddEmail.php" method="GET">

try this,
<form enctype="multipart/form-data" action="AddEmail.php" method="GET>
<table>
<tr>
<td>Email Address</td>
<td>First Name</td>
<td>Surname</td>
<td><td>
</tr>
<tr>
<td> <input type="text" name="email_address" value="example#email.com" > </td>
<td> <input type="text" name="firstName" value="First Name"> </td>
<td> <input type="text" name="surname" value="Surname"> </td>
<td> <input type="submit"> </td>
</tr>
</table>
</form>
<?php
$Email = $_GET["email_address"]
$firstName = $_GET["firstName"]
$surName = $_GET["surname"]
$sql = "INSERT INTO emailaddress (EmailAddress, FirstName, LastName) VALUES ($Email,$firstName, $surName)";
$queries = $conn->query($sql);
if ( $queries ) {
echo "New record created successfully";
}
else
{
die('Invalid query: ' . mysql_error());
}
$conn->close();
?>

<form enctype="multipart/form-data" action="AddEmail.php" method="GET>
to
<form enctype="multipart/form-data" action="AddEmail.php" method="post>

Related

Passing a variable from html to mysql with php

I'm trying to put info I get from a form in html into a mysql database by way of php and do it all on the same page. My code so far is thus
<?php
require('conn.php');
if( isset($_POST['send'])){
$Product_Name = htmlspecialchars($_POST["product_name"]);
$Stock = htmlspecialchars($_POST["stock"]);
$Price = htmlspecialchars($_POST["price"]);
$insert = "INSERT INTO product (Product_Name, Stock, Price) VALUES ('$Product_Name','$Stock','$Price')";
if (mysqli_query($conn,$insert)){
echo "Values inserted!\n";
}
else {
echo "Error inserting values: " . mysqli_error($conn);
}
}
mysqli_close($conn);
?>
<html>
<body>
<form action="insert.php" method="post">
<table border="1">
<tr>
<td>Product Name</td>
<td align="center"><input type="text" name="product_name" size= "30" /></td>
</tr>
<tr>
<td>In Stock</td>
<td align="center"><input type="text" name ="stock" size="30"/></td>
</tr>
<tr>
<td>Price</td>
<td align="center"><input type="text" name="price" size="30"/></td>
</tr>
<tr>
<td>Submit</td>
<td align="center"><input type="submit" value="send"></td>
<tr>
However when I try and load the page its just comes up blank. It used to at least show the form before I added in the php code but I can't pin down what I broke. What do I need to change so that this puts the users data into the database?
Edit: changed code based upon Jeffry's catches
You're missing the name attribute in your submit button declaration.
update
<input type="submit" value="send">
to
<input type="submit" name = "send" value="send">
just quick check, you miss the closing ) in
$Product_Name = htmlspecialchars($_POST["product_name"];
i also think you need a dot to append the string
$insert = "INSERT INTO product (Product_Name, Stock, Price) VALUES ("$Product_Name","$Stock","$Price")";
and if your product name is a varchar, you might need to quote it

how do fetching and then inserting from multi check boxes in php

I am going to fetching table values in a html table along checkbox in each row and then inserting values in another database table from multi check boxes in php.
Only the values of checked boxes should be submitted to that table.
db name "laboratory":
test: fetching values.
package: inserting table.
view
Status
Active
Inactive
<?php
$conn=mysqli_connect("localhost","root","","laboratory") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$query="SELECT * FROM test";
$result=mysqli_query($conn,$query);
if ($result) {
while ($record=mysqli_fetch_array($result)) {
Please try to follow this code and implement in your program . Hope that this will cooperate you much
if(isset($_POST['name'])){
$name = $_POST['name'];
$status = $_POST['status'];
if(empty($name) || empty($status)){
echo "Field Must Not be empty";
} else{
$conn=new mysqli("localhost","root","","test");
if($conn){
$query = "SELECT * FROM userdata limit 5";
$stmt = $conn->query($query);
$val = '<form action="" method=""> ';
$val .= '<table> ';
if ($stmt) { ?>
<form action="" method="post">
<table>
<?php while ($result=$stmt->fetch_assoc()) { ?>
<tr>
<td><?php echo $result['post']; ?></td>
<td><input value="<?php echo $result['post']; ?>" type="checkbox" name="check[]" /></td>
</tr>
<?php } ?>
<tr>
<td>Actual Price </td>
<td>Discount</td>
<td>Final Price</td>
</tr>
<tr>
<td><input type="text" name="actual"/></td>
<td><input type="text" name="discount"/></td>
<td><input type="text" name="final"/></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name="description" id="" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Cancel" /></td>
</tr>
</table>
</form>
<?php }} }}?>
<?php
if(isset($_POST)){
echo "<pre>";
print_r($_POST);
echo "<pre>";
}
?>`enter code here`
First of all you have to decide that what are you using either mysqli or mysql, if you are using mysqli then you have to improve your code
$query="SELECT * FROM test";
$result=mysqli_query($conn,$query);
if ($result) {
while ($record=mysqli_fetch_array($result)) {
and when you want to insert the checked data will be inserted in package table. If package table in another database then you have to give us the full detail i mean tell us the database name of package table.

inserting values in to database only once

<?php if(isset($_POST['submit']))
{
$tadd=$_POST["tadd"]; //getting values
$pname=$_POST["pname"];
$date=$_POST["date"];
$result=mysql_query("insert into pannel(tadd,pname,date)values('$tadd','$pname','$date')");
echo "<script type='text/javascript'>
alert('Quotation Generated Successfully!')
</script>";
} ?>
<center>
<h1>Title</h1>
</center>
<form name="form" method="post" action="" onSubmit="submit;">
<center><table border="0" cellspacing="0" style="width:350px">
<tr> <td><b>To Address</td> <td><textarea name="tadd" rows="5"
cols="30"></textarea></td></tr>
<tr> <td><b>Project Name</td> <td><input type="text" name="pname" required></td></tr>
<tr> <td><b>Date</td> <td><input type="text" name="date"
id="datepicker" required></td></tr>
<tr> <td colspan="2" align="center"><input type="submit" name="submit"
value="submit"/></td> </tr></center> </table> </form>
I have one record in my database with
id tadd pname date
1 hello vvv 22/10/2014
if i insert values into database again it should data already inserted
please help me regarding this issue
You can achieve this using mysql_num_rows() which is one way to do this, which I believe the goal is to avoid duplicates.
Sidenote: You can also set your column(s) as UNIQUE to avoid duplicates.
N.B.: I used the pname column as an example. It's up to you to check which one will always be unique in regards to a username for instance.
$query = "SELECT * FROM pannel where pname = '".$pname."'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0){
echo "Already exists.";
}
else{
mysql_query("insert into pannel (tadd, pname,date) values ('$tadd','$pname','$date')");
}
Do sanitize your data:
$tadd = mysql_real_escape_string($_POST["tadd"]);
and do the same for the others.
Even better, use mysqli with prepared statements, or PDO with prepared statements.
They're much safer, because your present code is open to SQL injection.
Footnotes:
You should get rid of onSubmit="submit;" in your form. As outlined in comments, it's not going to do anything.
Edit:
<?php
// assuming DB connection has been made.
if(isset($_POST['submit'])) {
$tadd= mysql_real_escape_string($_POST["tadd"]);
$pname= mysql_real_escape_string($_POST["pname"]);
$date= mysql_real_escape_string($_POST["date"]);
$query = "SELECT * FROM pannel where pname = '".$pname."'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0){
echo "Already exists.";
exit;
}
else{
mysql_query("insert into pannel (tadd, pname,date) values ('$tadd','$pname','$date')");
echo "<script type='text/javascript'>alert('Quotation Generated Successfully!')</script>";
}
} // brace for if(isset($_POST['submit']))
?>
<!DOCTYPE html>
<head></head>
<body>
<center><h1>Title</h1></center>
<form method="post" action="">
<div align="center">
<center>
<table border="0" cellspacing="0" style="width:350px">
<tr> <td><b>To Address</td> <td><textarea name="tadd" rows="5" cols="30"></textarea></td></tr>
<tr> <td><b>Project Name</td> <td><input type="text" name="pname" required></td></tr>
<tr> <td><b>Date</td> <td><input type="text" name="date" id="datepicker" required></td></tr>
<tr> <td colspan="2" align="center">
<input type="submit" name="submit" value="submit"/>
</td> </tr>
</table>
</center>
</div>
</form>
</body>
</html>

assign data from one table to a specific table (group)

I have a classrooms in schools and when I click on a certain classroom, I want to add students into it but my actual code is doing something stupid. It adds a student but i can see the student in all classrooms, not just in the one that i added him into. So when Im in classroom number 1, I see a form in there, I can add a student there, ... see how it works here:
here is the code: http://www.xxxx.xx/projekt/
here is my code in file trieda.php
<table align="center"><tr><td>
<form action="vlozit2.php" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="hidden" name="id_triedy" value="<?= $trieda['id_triedy'] ?>" />
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
</td></tr></table>
<?php
$result = mysqli_query($prip,"SELECT * FROM student ORDER BY meno");
while($student = mysqli_fetch_array($result))
{
echo "<br /><table cellspacing='1' cellpadding='1' class='tabulka1' align='center'><tr>";
echo "<td width='200'><a href='student.php?id_triedy=".$trieda['id_triedy']."".id_student=".$student['id_student']."'>".$student['meno']." ".$student['priezvisko']."</a></td>";
?>
<td width='300px' align='right' bgcolor="#fbfbfb">Zmazať</td>
</tr></table>
<?php
}
?>
here is vlozit2.php (a code that works for the form to add a student)
if(isset($_POST['submit']))
{
//meno a priezvisko
$student = $_POST['meno'];
$student = $_POST['priezvisko'];
$trieda = $_POST['id_triedy'];
//connect to the database
include 'config.php';
//insert results from the form input
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES('$_POST[meno]', '$_POST[priezvisko]', '$_POST[id_triedy]')";
$add = "<table align='center'>
<tr>
<td> Študent bol úspešne pridaný do triedy. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
$not_add = "<table align='center'>
<tr>
<td> Študent s týmto menom a priezviskom už je v tejto triede. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
if (mysqli_query($prip, $sql)) {
echo $add;
}else{
echo $not_add;
}
mysqli_close($prip);
}
?>
Try to replace your part of code with these snipets:
1) in trieda.php
<form action="vlozit2.php?id_triedy=<?php echo $_GET["id_triedy"];?>" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
2) in vlozit2.php
$student = $_POST['meno'];
$priezvisko = $_POST['priezvisko'];
$id_trieda = $_GET['id_triedy'];
and
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES( '{$student}', '{$priezvisko}', {$id_trieda} )";
Hopefully you store your id_trieda as INT type.
In your vlozit2.php file is nothing about inserting of class id. So put
<input type="hidden" name="classId" value="<?= $trieda['id'] ?>" />
to your form and in vlozit2.php get this value from $_POST['classId'] and insert it with other students data or anywhere you want to have it.

php my sql insert command didn't add the first column

I have this script but mysql insert section I have a problem the script didn't insert the first column "DNS" in sql table but did insert the second and the third one. what is my mistake the reason the first column escaped.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$all = implode(",",$_POST);
$all = explode(",",$all);
$insert = "INSERT INTO Cname VALUES (";
for ($x=0;$x<3;$x++)
{
$all[$x] = clean($all[$x]);
if ($all[$x] == "" || strlen($all[$x]) > 300)
die("<p>Incorect Data Entry, Please check again! Column - " . $x);
$insert .= "'" . $all[$x] . "',";
}
$insert .= "'')";
$res = mysql_query($insert) or die(mysql_error());
if($res)
die("<p>Succesfully Added! <a href='index.php'>Back To View Page</a></p>");
} else {
?>
<title>Add CNAME.</title>
</head>
<body>
<?php include("nav.php");?>
<div class="red"><p><h5>All fields are required.</h5></p></div>
<table id="box-table-a">
<tbody>
<form action="" method="POST">
<tr>
<td>DNS</td>
<!-- allow only the word CNAME -->
<td><input type="text" name="DNS" placeholder="enter a word 'CNAME" required pattern="CNAME" title="enter a word 'CNAME' on this field"></td>
</tr>
<tr>
<td>name</td>
<td><input type="text" name="name" placeholder="host with Full Domain Name" required pattern="^[a-zA-Z0-9\-]*$" title="host with full Domain Name"></td>
</tr>
<tr>
<td>alias</td>
<td><input type="text" name="alias" placeholder="only 3-15 digit are allowed" required pattern="^[a-zA-Z0-9-_\-]{3,15}*$" title="only 3-15 digit are allowed"></td>
</tr>
<tr>
<td><input type="submit" value="Add Cname"></td>
<td><input type="reset"></td>
</tr>
</tbody>
</table>
<?php
}
?>

Categories