Connect a form to a Mysql database table

Codes here !

Moderators: egami, macek, gesf

Post Reply
vitelus
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Feb 23, 2017 12:38 pm

Thu Feb 23, 2017 12:43 pm

Hey guys! I am new to this forum, first of all nice to meet you all!
I just started looking into PHP and I now I am about to aska really noobish question, but the thing is i searched online everywhere, i did what other people recommended but with no result.

I have this form:

<form action="clients.php" method="post" name="Clients">
<div class="form-group">
<label for="exampleInputFirstName">First Name</label>
<input type="text" class="form-control" id="FirstName" name="FirstName" placeholder="Enter First Name">
</div>
<div class="form-group">
<label for="exampleInputName">Name</label>
<input type="text" class="form-control" id="Name" name="Name" placeholder="Enter Surname">
</div>
</form>

clients.php:

<?php

$host_name = " ";
$database = " ";
$user_name = " ";
$password = " ";


$connect = mysqli_connect($host_name, $user_name, $password, $database);

if(mysqli_connect_errno())
{
echo '<p>Verbindung zum MySQL Server fehlgeschlagen: '.mysqli_connect_error().'</p>';
}
else
{
echo '<p>Verbindung zum MySQL Server erfolgreich aufgebaut.</p>';
}


$value=$_POST['FirstName'];
$sql="INSERT INTO Clients (fname) VALUES ('$value')";

if (!mysqli_query($sql)){
die('Error:' .mysqli_error());
}
mysqli_close();
?>

I of course entered the login info on the php, fname is the name of the row in my table, but I have 2 problems, first it gives an error on conection and also nothing transfers to my database...

Any help will be greatly appreciated!

chorn
php-forum Active User
php-forum Active User
Posts: 405
Joined: Fri Apr 01, 2016 2:18 am

Thu Feb 23, 2017 10:50 pm

vitelus wrote:
Thu Feb 23, 2017 12:43 pm
first it gives an error on conection
sooooo... which?

vitelus
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Feb 23, 2017 12:38 pm

Fri Feb 24, 2017 2:34 am

Fatal error: Uncaught Error: Call to undefined function mysql_query() in /homepages/0/d668512067/htdocs/clients.php:24 Stack trace: #0 {main} thrown in /homepages/0/d668512067/htdocs/clients.php on line 24

chorn
php-forum Active User
php-forum Active User
Posts: 405
Joined: Fri Apr 01, 2016 2:18 am

Fri Feb 24, 2017 4:30 am

so you posted the wrong file, all you are using is mysqli (which is right) but anywhere else your mixing in mysql.

vitelus
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Feb 23, 2017 12:38 pm

Fri Feb 24, 2017 4:49 am

I am really sorry but I am unable to understand:D, what exactly is wrong with my code ? I am just trying to make that form connect to my MySql table that i have from my web hosting provider and get the data from one part to the other...

chorn
php-forum Active User
php-forum Active User
Posts: 405
Joined: Fri Apr 01, 2016 2:18 am

Fri Feb 24, 2017 5:15 am

do not use mysql_query, use mysqli_query instead.

vitelus
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Feb 23, 2017 12:38 pm

Fri Feb 24, 2017 5:35 am

Thank you for that! I changed it everywhere to Mysqli and i still got the following error: "Error:" with no data sent to my table. Do you have like a sample php code so I can create an idea on how it should look.The above php code that I posted here is from a youtube tutorial and I don't know if it's correct

chorn
php-forum Active User
php-forum Active User
Posts: 405
Joined: Fri Apr 01, 2016 2:18 am

Tue Mar 07, 2017 5:03 am

just try to select all data afterwards.

Post Reply