4 Replies - 566 Views - Last Post: 09 May 2012 - 08:53 PM Rate Topic: -----

#1 syndicateapiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 30-January 12

Errro inserting data to MYSQL

Posted 09 May 2012 - 07:31 PM

Hello Guys..i have some problem with this matter..i've been searching at the forum for this problem, but still no luck on get this sql work..i hope u guys can tell me what is wrong with my codes..btw, i'm using WAMP and i got an error said like this

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

and this is my code

insert_sql.php

<?php

include 'registration.php';

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="bilboard"; // Database name 
$tbl_name="database"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$Nama_Pemilik = ($_POST['nama_pemilik']);
$Operator = ($_POST['operator']);
$Lokasi = ($_POST['lokasi']);
$Jenis_Iklan = ($_POST['jenis_iklan']);	


// Insert data into mysql 
$sql="INSERT INTO $tbl_name(Nama_Pemilik, Operator, Lokasi, Jenis_Iklan)VALUES('$Nama_Pemilik', '$Operator', '$Lokasi', '$Jenis_Iklan')";

$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";

}

else {
echo "ERROR INSERTING DATA";
}

// close connection 
mysql_close();
?>



and for the form

registration.php

<head>
</head>

<body bgcolor="#999999">
<img src="Bilboard2.jpg">
<form name="registration" method="post" action="insert_sql.php">
<table width="1000" bgcolor="#9999FF">
<tr>
<td height="100" width="1000">
<h2 align="center">REGISTRATION FORM</h2>
<hr>
</td>
</tr>
</table>
<table width="1000" bgcolor="#9999FF">
<tr>
<td width="232">Nama Pemilik : </td>
<td width="756"><input type="text" name="nama_pemilik"></td>
</tr>

<tr>
<td>Operator : </td>
<td><input type="text" name="operator"></td>
</tr>

<tr>
<td>Lokasi :</td>
<td><input type="text" name="lokasi"></td>
</tr>

<tr>
<td>Jenis Iklan : </td>
<td><input type="text" name="jenis_iklan"></td>
</tr>



<td><input type="submit">
<input type="reset"></td>
</table>

</form>
</body>
</html>



Hope u guys can help me with this..thanks..

This post has been edited by syndicateapiz: 09 May 2012 - 07:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Errro inserting data to MYSQL

#2 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Errro inserting data to MYSQL

Posted 09 May 2012 - 08:15 PM

View Postsyndicateapiz, on 10 May 2012 - 10:31 AM, said:

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


should be something like

$connection = mysql_connect($host, $username, $password)or die(mysql_error()); 
$db = mysql_select_db($db_name,$connection)or die(mysql_error());



or similar to start with, leave in the mysql_error function in future as it will help locate were the error is.

repost the error code and any more problems.

also
line 25:

$result = mysql_query($sql) or die (mysql_error());


This link may be useful also

This post has been edited by floppyspace: 09 May 2012 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 syndicateapiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 30-January 12

Re: Errro inserting data to MYSQL

Posted 09 May 2012 - 08:25 PM

its making more error

if i put double brackets like this

$connection = mysql_connect($host, $username, $password)or die(mysql_error());
$db = mysql_select_db($db_name,$connection)or die(mysql_error());


its giving me error like this

syntax error, unexpected )

if i put single brackets, it still show the same error like this

( ! ) Notice: Undefined index: nama_pemilik in C:\wamp\www\bilboard\insert_sql.php on line 14

and the same for all the table..
Was This Post Helpful? 0
  • +
  • -

#4 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Errro inserting data to MYSQL

Posted 09 May 2012 - 08:49 PM

sorry been a while since I played with db connections,you should be using a separate config.php or similar with your connection details in it so they are hidden.

This and this are simple example that does not take into account sql injections/security but will get you on the right track, looking at your code you need to make a few adjustments in a couple of areas.

Someone else may come along and point them all out but those links should fix your issues.

This post has been edited by floppyspace: 09 May 2012 - 08:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 syndicateapiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 30-January 12

Re: Errro inserting data to MYSQL

Posted 09 May 2012 - 08:53 PM

Thanks for the link u given to me but i've been to through multiple website including that u giving me, but still no luck..i dont know if my WAMP causing this..i didnt multiple trail-and-error still giving me the same error..i'm stucked at the same problem..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1