Jump to content

Recommended Posts

hey guys i've been copying alot of free php scripts in the web but  this last one i keep getting  the error message.  it won't let me connect . here are the scripts.

 

(page1)

 

insert.php

<html>

<head>

<title>Untitled Document</title>

</head>

<body>

 

<form name="form1" action=" insert_ac.php" method="post">

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">

<tr>

<td>

<table width="100%" border="0 cellspacing="1" cellpadding="3">

<tr>

<td colspan="3"><strong>Insert Data Into mySQL Database</strong></td>

</tr>

<tr>

<td width="71">Name</td>

<td width=""6>:</td>

<td width="301">

<input name="name" type="text" id="name">

</td>

</tr>

<tr>

<td>Lastname</td>

<td>:</td>

<td>

<input name="lastname" type="text" id="lastname"></td>

</tr>

<tr>

<td>Email</td>

<td>:</td>

<td><input name="email" type="text" id="email"></td>

</tr>

<tr>

<td colspan="3" align="center"><input type ="submit" name="Submit" value="Submit"></td>

</tr>

</table>

</form>

</body>

</html>

 

(page2)

 

insert_ac.php

<?php

 

//Connecting to server and selecting databaase.

 

mysql_connect("localhost", "root", "")or die("cannot connect");

mysql_select_db("test_create_db") or die("cannot select DB");

 

//Get values from form.

 

$name=$_POST['name'];

$lastname=$_POST['lastname'];

$email=$_POST['email'];

 

//Insert data into mysql

$sql="INSERT INTO web_members(name,lastname,email)VALUES($name,$lastname,$email)";

$result=mysql_query($sql);

 

//if succesfully insert data into database,display message "Successful".

if($result){

echo "Successful";

echo "<BR>";

echo "<a href='insert.php'>Back to main page</a>";

}

else{

echo "ERROR";

}

 

//close connection

mysql_close();

 

?>

Link to comment
https://forums.phpfreaks.com/topic/236486-help-plz/
Share on other sites

yes i have the database set up and the error i keep getting  its the one  thar i have made bold.

 

($result){

echo "Successful";

echo "<BR>";

echo "<a href='insert.php'>Back to main page</a>";

}

else{

echo "ERROR";<-

}

 

//close connection

mysql_close();

 

 

here is  my  data base config

 

<?php

$host=mysql_connect("localhost", "root", "")or die("cannot connect to server");

mysql_select_db("test_create_db")or die("connot select db");

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/236486-help-plz/#findComment-1215786
Share on other sites

try this instead:

 

change your connection string to:

 

$db=mysql_connect("localhost", "root", "pass...")or die("cannot connect");

You sure you have root access or is that what you named your username?  that is a bad idea if you named your username that...

 

then change your query to:

$sql=mysql_query("INSERT INTO web_members(name,lastname,email)   VALUES('$name','$lastname','$email' ",$db);

 

* put a space before "VALUES"

 

if you still get an error, its either your connection itself or the query not having values, or not agreeing with your table web_members.  if your still having issues try doing this after your insert:

echo "insert statement: INSERT INTO web_members(name,lastname,email) VALUES('$name','$lastname','$email'  <br>";

at least you can see your insert and then run it directly on the db to see if you get a syntax issue.  you could also always do or die commands afterwards and that would tell you if your sql syntax is wrong.

Link to comment
https://forums.phpfreaks.com/topic/236486-help-plz/#findComment-1215828
Share on other sites

thanks guys for all your help i found the mistake :). I didn't put the single colons  before, in between, and after the variables on the values and i forgot the space as well.

 

//Insert data into mysql

the error was on this line->$sql="INSERT INTO web_members(name,lastname,email)VALUES($name,$lastname,$email)";

 

the correct way was like this ->$sql="INSERT INTO web_members(name, lastname, email)  VALUES ('$name','$lastname','$email')";

$result=mysql_query($sql);

 

 

Link to comment
https://forums.phpfreaks.com/topic/236486-help-plz/#findComment-1215844
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.