Jump to content

Need Help with mysql query in PHP [URGENT] !


Nishchay

Recommended Posts

Hey guys i'am basically making a database driven website and i need to provide my customers a interactive form on the website . When they enter their details their info should enter my database.
for that purpose i firstly have connected my database to my webpage.
now i want this :
user should type firstname and lastname in a form and that should enter my fname and lname attribute in my table Test in the database. i tried doing this but it didnt work out



I've made a html file ( k.html) whose code is below :

<html>
<body>

<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
<input type="submit">
</form>

</body>
</html>

Now i've made an insert.php file whose code is below :


<?php
$c=mysql_connect("localhost","username","password") // (My personal)
if(!$c)
{
echo("Unable to connect to the database at this time :( ");
}
else
{
echo("Connection to the database was succesfull :)");
}
?>

 

<?php
mysql_select_db("a6279515_nis",$c);
if(!mysql_select_db("a6279515_nis",$c))
{
echo("Can't establish database");
}
?>

 

 

<?php
$sql="INSERT INTO Test (fname,lname)VALUES('$_POST[firstname]','$_POST[lastname]');
if (!mysqli_query($c,$sql))
{
die('Error: ' . mysqli_error());
}
echo "1 record added";

mysqli_close($c);
?>




The error which my page shows is that :



Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in /home/a6279515/public_html/insert.php on line 21


Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/a6279515/public_html/insert.php on line 23

Link to comment
Share on other sites

Please read the following post on how to debug your SQL errors.

 

Also, you should be using either MySQLI(mproved) or PDO. Seeing as the old mysql library is no longer maintained and deprecated in 5.5.

 

PS: Labelling a post "urgent" will not get you faster help. If anything, it might cause people to skip it. If you need urgent help, then the best bet is to pay someone for it. On a forum all threads have the same priority, as it is a purely voluntary effort from those who help.

Link to comment
Share on other sites


<?php

$con=mysql_connect("localhost","database_name","database_password")or die("mysql_errow()");
$db=mysql_select_db("database_name",$con)or die("mysql_errow()");




if($_POST['submit']){


$firstname=mysql_real_escape_string($_POST['firstname']);
$lastname=mysql_real_escape_string($_POST['lastname']);


if( empty($firstname) || empty($lastname)){

echo" Sorry you need to enter a firstname and lastname";

die();

}

$insert="INSERT INTO database_field(firstname,lastname)VALUES('$fistname','$lastname');
$insert_query=mysql_query($insert)or die("database error");

if($insert){

echo" The database has your $firstname and $lastname cheers";

)else{

echo" sorry there a problam some where email me @ what ever.com";

}

?>

<html>
<head>
<title>submit username and lastname</title>
</head>

<body>

<table>
<tr>
<td>
<form method="POST" action="">
<br />
<br />
Please add your name.
<input type="text" name="firstname">
<br />
<br />
Please add your surname.
<input type="text" name="lastname">
<br />
<br />
<input type="submit" name="submit">
</td>
</tr>
</table>
<br />

</body>
</html>


Not tested at all.

Edited by redarrow
Link to comment
Share on other sites

<?php

$con=mysql_connect("localhost","username","password")or die("mysql_errow()");

$db=mysql_select_db("database_name",$con)or die("mysql_errow()");


if($_POST['submit']){

$firstname=mysql_real_escape_string($_POST['firstname']);
$lastname=mysql_real_escape_string($_POST['lastname']);

if( empty($firstname) || empty($lastname)){
echo" Sorry you need to enter a firstname and lastname  <a href='".$_SERVER['PHP_SELF']."'>TRY AGIN</a>";
die();
}

$insert="INSERT INTO database_field(firstname,lastname)VALUES('$firstname','$lastname')";
$insert_query=mysql_query($insert)or die('database error()');

if($insert_query){
echo" The database has your $firstname and $lastname cheers";
}else{
echo" sorry there a problam some where email me @ what ever.com";
}
 }
?>
<html>
<head>
<title>submit username and lastname</title>
</head>
<body>
<table>
<tr>
<td>
<form method="POST" action="">
<br />
<br />
Please add your name.
<br />
<input type="text" name="firstname">
<br />
<br />
Please add your surname.
<br />
<input type="text" name="lastname">
<br />
<br />
<input type="submit" name="submit">
</td>
</tr>
</table>
<br />
</body>
</html>

version 2 complete ... 10/10 i must say my self.

Edited by redarrow
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.