Submitting form data to MySQL

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,413
Reaction score
7,522
Location
Bellville
Hi, can someone please help me here I am not sure if it is me making an mistake or if it is something else.

I tried a lot of deffirent methods now from 1 form item to a multiple form item, but no matter what I do it doesn't seem to send the data to the database.

Example:

HTML:
<form class="form-horizontal" id="form_members" role="form" action="test.php" method="POST">
<legend>Person</legend>
<div class="form-group">
    <label for="firstname" class="col-sm-2">First Name</label>
    <div class="col-sm-4">
    	<input type="text" class="form-control" name="firstname" id="firstname" placeholder="First Name">
    </div>
    <label for="lastname" class="col-sm-2">Last Name</label>
    <div class="col-sm-4">
    	<input type="text" class="form-control" name="lastname" id="lastname" placeholder="Last Name">
    </div>
</div>
<div class="form-group">
    <label for="gender" class="col-sm-2">Gender</label>
    <div class="col-sm-4">
        <label class="radio-inline">
        <input type="radio" name="gender" id="male" value="male"> Male
        </label>
        <label class="radio-inline">
        <input type="radio" name="gender" id="female" value="female"> Female
        </label>
    </div>
    <label for="dob" class="col-sm-2">Date of Birth</label>
    <div class="col-sm-4">
        <input type="date" class="form-control" name="dob" id="dob" placeholder="mm/dd/yyyy">
    </div>
</div>
<legend>Address</legend>
<div class="form-group">
    <label for="address" class="col-sm-2">Address</label>
    <div class="col-sm-4">
        <input type="text" class="form-control" name="address" id="address" placeholder="Street + No">
    </div>
    <label for="city" class="col-sm-2">City</label>
    <div class="col-sm-4">
    <input type="text" class="form-control" list="cities" name="city" id="city" placeholder="City">
    <datalist id="cities">
        <option value="Aquila">
        <option value="Aosta">
        <option value="Bari">
        <option value="Venezia">
    </datalist>
    </div>
</div>
<div class="form-group">
    <label for="zipcode" class="col-sm-2">ZIP Code</label>
    <div class="col-sm-4">
        <input type="text" class="form-control" name="zipcode" id="zipcode" placeholder="ZIP Code">
    </div>
    <label for="region" class="col-sm-2">Region</label>
    <div class="col-sm-4">
        <select class="form-control" name="region" id="region">
            <option>Select a Region</option>
            <option>Abruzzo</option>
            <option>Aosta</option>
            <option>Apulia</option>
            <option>Veneto</option>
        </select>
    </div>
</div>
<legend>Contact Info</legend>
<div class="form-group">
    <label for="phone" class="col-sm-2">Phone</label>
    <div class="col-sm-4">
        <input type="tel" class="form-control" name="phone" id="phone" placeholder="Phone Number">
    </div>
    <label for="email" class="col-sm-2">Email</label>
    <div class="col-sm-4">
        <input type="email" class="form-control" name="email" id="email" placeholder="Email">
    </div>
</div>
<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-warning" name="submit" id="submit">Submit</button>
    </div>
</div>
</form>

PHP:
<?php
$link = mysqli_connect("localhost","sc_thor","Thor@123")  or die("failed to connect to server !!");
mysqli_select_db($link,"sc_resellers");
if(isset($_REQUEST['submit']))
{
$errorMessage = "";
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$gender=$_POST['gender'];
$dob=$_POST['dob'];
$address=$_POST['address'];
$city=$_POST['city'];
$zipcode=$_POST['zipcode'];
$region=$_POST['region'];
$phone=$_POST['phone'];
$email=$_POST['email'];
 
// Validation will be added here
 
if ($errorMessage != "" ) {
echo "<p class='message'>" .$errorMessage. "</p>" ;
}
else{
//Inserting record in table using INSERT query
$insqDbtb="INSERT INTO `sc_resellers`.`members`
(`firstname`, `lastname`, `gender`, `dob`, `address`,
`city`, `zipcode`, `region`, `phone`, `email`) VALUES ('$firstname', '$lastname', 
'$gender', '$dob', '$address', '$city', '$zipcode', '$region', '$phone', '$email')";
mysqli_query($link,$insqDbtb) or die(mysqli_error($link));
}
}
?>


If possible can some send me just a different small working example of a form and then I will upload it to my server and see if it works.


If I use my php mail function then it works, it's just the writing to the DB that doesnt want to work and I am probably doing something wrong.
 
Not a php expert, but isn't there a mysqli_execute you should use instead of query?
 
Have you checked the logs?

Note that mysqli_connect takes a 4th param for "database". You don't need to use mysqli_select_db.

Maybe consider a mysqli wrapper with built in error checking etc.
 
Not a php expert, but isn't there a mysqli_execute you should use instead of query?

tried both no luck

Have you checked the logs?

Note that mysqli_connect takes a 4th param for "database". You don't need to use mysqli_select_db.

Maybe consider a mysqli wrapper with built in error checking etc.

I am a noob you'll either have to spell it out in English or send me a working example to test.

I have tried both:

adding the DB to the 4 para and then out of desperation

PHP:
mysqli_select_db($link,"sc_resellers");

no luck
 
Last edited:
echo your insert statement and then run it directly in mysql to see what it does

unfortunately we can't tell you what the matrix is neo.... you have to develop the skills to debug on your own to have a moderate to good chance of becoming a skilled programmer some day...
 
Remove the parentheses from the column names as well as the variable names in teh values section.
 
As hamster suggested you need to use an exec() function. Query function is used to return from your DB. I cannot help you as I come from a PDO background.
In future use try catch for error handling. Also what you are doing in your code makes it prone to sql injection. You can visit http://www.owasp.org/ for writing secure code and read more about prepared statements: http://www.w3schools.com/php/php_mysql_prepared_statements.asp
 
Last edited:
As hamster suggested you need to use an exec() function. Query function is used to return from your DB. I cannot help you as I come from a PDO background.
In future use try catch for error handling. Also what you are doing in your code makes it prone to sql injection. You can visit http://www.owasp.org/ for writing secure code and read more about prepared statements: http://www.w3schools.com/php/php_mysql_prepared_statements.asp

Execute is depreciated.

Anyway this works perfectly on Xamp on the local install and DB

For some reason on the VPS it doesn't work so my code is not the problem, cool I'll go dig on the server why it doesn't work.
 
Execute is depreciated.

Anyway this works perfectly on Xamp on the local install and DB

For some reason on the VPS it doesn't work so my code is not the problem, cool I'll go dig on the server why it doesn't work.

Many of the points I mentioned does not apply to MySQLi after reading _neo's link. But I think it's better to move to PDO.
 
make sure of the database name and table structure are the same as your local on your VPS, and try to use "127.0.0.1" instead of "localhost" (if PHP is connecting to MySQL locally)... and check your IIS/Apache/PHP log file

You should definitely move to PDO, its good to know the legacy connections though (mysql_* & mysqli_*)
 
make sure of the database name and table structure are the same as your local on your VPS, and try to use "127.0.0.1" instead of "localhost" (if PHP is connecting to MySQL locally)... and check your IIS/Apache/PHP log file

You should definitely move to PDO, its good to know the legacy connections though (mysql_* & mysqli_*)

I just started literally learning as I go.

Thank you I will look on the apache logs etc and report back
 
https://www.youtube.com/watch?v=n35Jn2nP9iU
try this video on youtube its exactly what you are doing
If you look at his connect to $mysqli= newmysqli ('localhost' , ' yourusername ' , 'yourpassword' , 'yourdatabasename');
all your code points to a different place
Also look at his code structure.
Learnt a lot from his videos
 
Top
Sign up to the MyBroadband newsletter
X