I'm sorry if this has been mentioned many times in the thread but I just want to ko some answer. Can someone tell me how I got an error like this
Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\create.php on line 8
Here is my code:
<?php
mysql_connect("localhost","root");
mysql_select_db("comment");
if(isset($_POST['submit']))
(
$Name = $_POST['name'];
$Email = $_POST['email'];
mysql_query("INSERT INTO demo (c_name,c_email) VALUES ('$Name','$Email')");
echo "Inserted !!!";
)
?>
<form method = "post" action = "">
<div>
<label for="name">Your Name</label>
<input type="text" name="name" id="name">
<label for="email">Your Email</label>
<input type="text" name="email" id="email">
<label for="submit">Submit</label>
<input type="submit" name="submit" id="submit">
</div>
</form>
I'm pretty sure the php codes are correct so I ended up in confusion.
P.S. I've acknowledged that mysql_function are outdated now but I don't have the luxury to study for PDO or mysqli_ this semestral end term. Thanks
if clauses in PHP have to be surrounden with curly brackets:
if(statement) { /* your code goes here */ }
Your code works this way:
if(isset($_POST['submit']))
{
$Name = $_POST['name'];
$Email = $_POST['email'];
mysql_query("INSERT INTO demo (c_name,c_email) VALUES ('$Name','$Email')");
echo "Inserted !!!";
}
BTW [OT]: beware of SQL injections.
I am not sure why people don't use any IDE like phpStorm or NetBeans
If you were using an IDE you should see something like this and never post this question:
You can guess something is wrong with (
<?php
mysql_connect("localhost","root");
mysql_select_db("comment");
if(isset($_POST['submit'])) {
$Name = $_POST['name'];
$Email = $_POST['email'];
mysql_query("INSERT INTO demo (c_name,c_email) VALUES ('$Name','$Email')");
echo "Inserted !!!";
}
?>
<form method = "post" action = "">
<div>
<label for="name">Your Name</label>
<input type="text" name="name" id="name">
<label for="email">Your Email</label>
<input type="text" name="email" id="email">
<label for="submit">Submit</label>
<input type="submit" name="submit" id="submit">
</div>
</form>
Related
I am building a simple client management system that allows users to input client data into a form populated in a modal. The idea is that when the user completes the form in the modal by clicking Create New Client, new client information will be stored into the predefined mysql table named client. I am using a nearly identical php script throughout the site to create new users which works fine.
The primary difference with this script is that is called from a form that is enclosed within a modal that is enclosed within divs. Could that possibly be inhibiting my scripting from following through on the query?
There are no error messages other than my own defined if else calls. The data will echo on Submit but information will not actually insert into the data in mysql. I'm still new to anything beyond html and css, so any help will be greatly appreciated and reciprocated as I continue to get a handle on this journey. Reference code below
newclient.php
<?php
session_start();
include 'dbh.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
$comname = $_POST['comname'];
$primcon = $_POST['primcon'];
$addr = $_POST['addr'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phn = $_POST['phn'];
$websi = $_POST['websi'];
$email = $_POST['email'];
$activ = $_POST['activ'];
$sql = "INSERT INTO client (comname, primcon, addr, city, state, zip, phn,
websi, email, activ )
VALUES ('$comname', '$primcon', '$addr', '$city', '$state', '$zip', '$phn',
'$websi', '$email', '$activ')";
$result = mysqli_query($conn, $sql);
echo $comname;
echo $primcon;
echo $addr;
echo $city;
echo $state;
echo $zip;
echo $phn;
echo $websi;
echo $email;
echo $activ;
//header("Location: login.html");
admin.html - where users actually create new clients (modal content only)
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>New Client Registration</h2>
</div>
<div class="modal-body">
<form align="center"
action="http://localhost:1234/housenotes/newclient.php" method="POST">
<input type="text" placeholder="Company Name" name="comname"
required>
<input type="text" placeholder="Primary Contact" name="primcon"
required>
<input type="text" placeholder="Street Address" name="addr"
required> <br>
<input type="text" placeholder="City" name="city" required>
<input type="text" placeholder="State" name="state" required>
<input type="text" placeholder="Zip" name="zip" required> <br>
<input type="text" placeholder="Phone Number" name="phn"
required>
<input type="text" placeholder="Website" name="websi" required>
<input type="text" placeholder="Email" name="email" required>
<br>
<input type="radio" name="activ" value="Potential" checked>
Potential
<input type="radio" name="activ" value="Engaged"> Engaged
<input type="radio" name="activ" value="Active"> Active<br>
<button type="submit" class="button" style="width:15%;">Create
New Client</button>
</form>
</div>
<div class="modal-footer">
<h3>housenotes</h3>
</div>
Again, any help would be greatly appreciated! Thanks in advance!
I have created a HTML form which will need to insert the data that was entered into the form straight into a table in mySQL.
newuser.php file
<?php
//including the connection page
include('./DB_Connect.php');
//get an instance
$db = new Connection();
//connect to database
$db->connect();
//fetch username and password
$usertype = $_POST['userType'];
$firstname = $_POST['firstName'];
$lastname = $_POST['lastName'];
$username = $_POST['userName'];
$password = $_POST['password'];
$address1 = $_POST['add1'];
$address2 = $_POST['add2'];
//write the sql statement
$query = "INSERT INTO USERS (usertype, fname, lname, username, password, add1, add2)
VALUES ('$usertype', '$firstname', '$lastname', '$username', '$password', '$address1', '$address2')";
mysql_query($query,$db);
if (($query) == TRUE) {
echo "New record created successfully";
header("Location: login.php", true);
exit();
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
header("Location: register.php", true);
exit();
}
//close once finished to free up resources
$db->close();
?>
With the following html form:
<form action="newuser.php" method="POST" class="form" id="registerForm">
<label> Type of user: </label> <br>
<input type="radio" name="userType" id="itemSeeker">Item Seeker </input>
<input type="radio" name="userType" id="itemDonor">Item Donor </input>
<input type="radio" name="userType" id="peopleSeeker">People Seeker </input>
<input type="radio" name="userType" id="peopleDonor">People Donor </input>
<br>
<br>
<div class="form-inline">
<div class="form-group">
<input type="text" name="firstName" placeholder="First Name: " align="center" class="form-control" ></input>
</div>
<div class="form-group">
<input type="text" name="lastName" placeholder="Last Name: " align="center" class="form-control" ></input>
</div>
</div>
<br>
<input type="text" name="email" placeholder="Email Address: "align="center" class="form-control" ></input>
<br>
<div class="form-inline">
<div class="form-group">
<input type="text" name="userName" placeholder="Username: " align="center" class="form-control" ></input>
</div>
<div class="form-group">
<input type="password" name="password" placeholder="Password: " align="center" class="form-control" ></input>
</div>
</div>
<br>
<!-- <label> Address 1: </label>-->
<input type="text" name="add1" placeholder="Address 1: " align="center" class="form-control" ></input>
<!-- <label> Address 2: </label>-->
<input type="text" name="add2" placeholder="Address 2: " align="center" class="form-control" ></input>
<br>
<button class="btn btn-primary" name="submitReg" type="submit">Submit</button><br>
<br>
<a href="login.php" >Already have an account?</a>
</form>
The USERS table
The above two blocks of code and the code that I'm working with.
My problem here is, when the form is submitted, the data isn't actually being entered into the table. Note that the first ever submission actually did work. All submissions after the first one don't seem to be entering anything into the database.
I'm not quite sure what's wrong with my code for it to not work. It does go to the 'login.php' page which means there aren't any faults and the query submitted correctly.
Could someone please tell me what I'm doing wrong, thank you.
right now you have alot more trouble than your insert problem. your code is totaly insecure. (mysql injections).
Dont use mysql_* functions use pdo instead with prepared statements!
you output spaces before you send a header you cant send header if you have output. your redirect wouldnt work. dont relay on a clientside redirect use may have it disabled so output a link where you want the user to go.
anotherthing your radio buttons have no value check html syntax
var_dump($_POST) and check if you submit everything. also check for isset or empty befor assign variables. do some sort of validation.
have a look at some php frameworks they provide much more flexibility and error checking
dont reinvent the wheel by writing everthing by your own in a 10 or more year behind procedural way
So I am working on this under construction page for my own small business's website. I want to have an email field so that users write their email so that i can later tell them when the page is finished. But I am having trouble with the database. Apparently the connection is established but the data is not being written to the table in the DB. This is my php code, it is embedded on the top of my index.html:
<?php require 'connections/connections.php'; ?>
<?php
if(isset($_POST['submit'])) {
$Fname = $_POST['first_name'];
$Lname = $_POST['last_name'];
$Email = $_POST['email'];
$sql = $con->query("INSER INTO subscriptions (Fname, Lname, Email)Values('{$Fname}', '{$Lname}', '{$Email}')");
header ('Location: confirm.html');
}
?>
This is my connections.php file, required by the above code:
<?php
$con = mysqli_connect("localhost", "webmaster_velez", "Cristianpromw3", "nexus_subscriptions");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
This is the HTML that the user interacts with to send their email address:
<div id="subscribe">
<form id="SubscriptionForm" name="RegisterForm" method="post">
<div class="FormElement">
<input name="first_name" type="text" required="required" class="TField" id="first_name" form="SubscriptionForm" placeholder="First Name">
</div>
<div class="FormElement">
<input name="last_name" type="text" required="required" class="TField" id="last_name" form="SubscriptionForm" placeholder="Last Name">
</div>
<div class="FormElement">
<input name="email" type="email" required="required" class="TField" id="email" form="SubscriptionForm" placeholder="Email">
</div>
<div class="FormElement">
<input name="submit" type="submit" class="button" id="submit" form="SubscriptionForm" value="Submit">
</div>
</form>
</div>
Any help, corrections, and/or advice on this would be much appreciated and accepted. Thanks in advance.
PS. I am new to PHP and database/server dynamics.
From reading the code you've written, I would assume that the main problem you are facing is this typo - "INSER INTO" should be "INSERT INTO".
You'll also need to implement some try error blocks to catch if any errors are happening and protect yourself from SQL vulnerabilities/injections.
I would like to get some user data using a php form and store it in a mysql database , trouble is, the page simply refreshes when I submit the form.
Here is my php form:
<form id="companyform" name="companyform" method="post" action="index3.php" data-ajax="false">
<b>To enlist your business fill in the form below:</b>
<p>
<label for="name">Company Name:</label>
<input type="text" name="companyname" id="companyname" data-mini="true"/>
</p>
<p>
<label for="name">Company Address:</label>
<input type="text" name="companynaddress" id="companynaddress" data-mini="true"/>
</p>
<p>
<label for="textfield">Tel No.:</label>
<input type="text" name="tel" id="tel" data-mini="true"/>
</p>
<p>
<label for="textfield">Fax No.:</label>
<input type="text" name="fax" id="fax" data-mini="true"/>
</p>
<p>
<label for="textfield">Email:</label>
<input type="text" name="email" id="email" data-mini="true"/>
</p>
<p>
<label for="textfield">Website Address:</label>
<input type="text" name="website" id="website" data-mini="true"/>
</p>
<p>
<label for="textfield">Contact Person Name:</label>
<input type="text" name="contactname" id="contactname" data-mini="true"/>
</p>
<p>
<label for="textfield">Contact Person Number:</label>
<input type="text" name="contactnumber" id="contactnumber" data-mini="true"/>
</p>
<p>
<label for="textfield">Contact Person Email:</label>
<input type="text" name="contactemail" id="contactemail" data-mini="true"/>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Submit" />
</p>
</form>
and here is my database connection code:
<?php
if (array_key_exists('submit', $_POST)) {
$con = mysql_connect("host", "user", "pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("botswanasearchdb", $con);
$companyname = $_POST['companyname'];
$companyaddress = $_POST['companyaddress'];
$tel = $_POST['tel'];
$fax = $_POST['fax'];
$emailid = $_POST['emailid'];
$website = $_POST['website'];
$contactname = $_POST['contactname'];
$contactnumber = $_POST['contactnumber'];
$contactemail = $_POST['contactemail'];
// prepare the SQL query
$sql = "INSERT INTO businessuser (companyname, companyaddress, tel, fax, emailid, website, contactname, contactnumber, contactemail) VALUES ('$companyname', '$companyaddress', '$tel', '$fax', '$emailid', '$website', '$contactname', '$contactnumber', '$contactemail')";
mysql_close($con);
}
?>
This is your form tag:
<form id="companyform" name="companyform" method="post" action="index3.php" data-ajax="false">
So you are submitting the form to index3.php (the action attribute). According to your comment index3.php contains your form and that is why the form refreshes when you submit it. You are basically reloading your form on form submit.
You need to submit the form to your php script that contains the php code you posted.
Edit: If everything is on the same page, you can do something like:
if (array_key_exists('submit', $_POST))
{
// your code
// show thank you message
}
else
{
// show form
}
Another edit: As you are using the deprecated mysql_* functions and not escaping the data, you have an sql injection whole and a ' character in your data will break your query. You should switch to PDO / mysqli and prepared statements. And always add error handling.
Here is the code:
contact_us2.php
<form id="form1" method="post" action="enquires.php">
<fieldset>
<legend>Form to database example</legend>
<label for="text">
<span>Comments:</span>
<textarea id="text" name="comments" rows="4" cols="80"></textarea>
</label>
<label for="name">
<span>Name:</span>
<input id="name" type='text' name='name' size='50'/>
</label>
<label for="email">
<span>Email:</span>
<input id="email" type='text' name='email' size='50'/>
</label>
<label for="submit1" id="submit"><span> </span>
<input id="submit1" class="submit" type="submit" name="submit" value="Submit"/>
</label>
</fieldset>
</form>
enquires.php
<?php
session_start();
error_reporting(E_ALL & ~E_NOTICE);
require('authenticate.php');
include_once"../scripts/connect_to_mysql.php";
$name = $_post['name'];
$email = $_post['email'];
$comments = $_post['comments'];
echo "$email";
// Query the body section for the proper page
mysql_select_db("hardware_cms" )or die (mysql_error());
$sqlCommand = MYSQL_QUERY("INSERT INTO enquires (id, name, email, comments)". "VALUES ('NULL', '$name', '$email', '$comments')") or die (mysql_error());
?>
There is no errors. The only thing that is inserted into the database is the id. I think the problem is in contact_us2.php. I'm new to html and php sorry if this is a silly question.
The error is in the php code because you have to use $_POST and don't $_post
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
Replace $_post with $_POST and see what happens.
Also, PLEASE sanitize your SQL inputs (either by using mysql_real_escape_string, or better - by using PDO prepared statements).