0

I'am trying to do very simple php page and have some kind of problem here. the page works and everything but there is nothing coming to mysql. When I press submit it prints insert into DrinkHistory(CustomerId,DrinkId,SellerId) values('1','3','2')Your Data Inserted. But when I login to phpmyadmin there is nothing in DrinkHistory table.

the code is below

<?php
// Create connection
$con=mysqli_connect("localhost","******","*******","test");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Mene kotii ku et mitää osaa: " . mysqli_connect_error();
  }

// Insert Data

@$a=$_POST['CustomerId'];
@$b=$_POST['DrinkId'];
@$c=$_POST['SellerId'];
if(@$_POST['submit'])
{
echo $s="insert into DrinkHistory(CustomerId,DrinkId,SellerId) values('$a','$b','$c')";
echo "Your Data Inserted";
mysql_query($s);
}

?>


<center>
<form method="post">
<table width="100%" height="245" border="1" bgcolor="#00CCFF">
<tr><td width="112" height="26">Asiakas</td>
<td width="100"><input type="radio" name="CustomerId" value="1"/>Jokirinne Niko</td>
</tr>

<tr><td rowspan="2">Juoma</td>
<form method)="post">
<td height="28"><input type="radio" name="DrinkId" value="2"/>Kalja</td>
<td height="28"><input type="radio" name="DrinkId" value="3"/>Lonkero</td>
<td height="28"><input type="radio" name="DrinkId" value="4"/>Siideri</td>
<td height="28"><input type="radio" name="DrinkId" value="5"/>Fisu</td>
<td height="28"><input type="radio" name="DrinkId" value="6"/>Tequila</td>
<td height="26"><input type="radio" name="DrinkId" value="7"/>MustikkaShotti</td>
</tr>
<td height="33"></tr>


<tr><td rowspan="3">Myyjä</td>
<td><input type="radio" name="SellerId" value="1"/>Niko Jokirinne</td>
<tr>
<td><input type="radio" name="SellerId" value="2"/>Tanya Lickorish</td>
<tr>

<tr><td height="62"><input type="submit" name="submit" value="Juo"/></td></tr>
</table>
</form>
</center>
</body>
</html>
Vivek
  • 1,640
  • 1
  • 17
  • 34

2 Answers2

0

You have several database libraries to choose from but, once you decide, you need to stick to one. You cannot start a connection with the new mysqli extension:

$con=mysqli_connect("localhost","******","*******","test");

... and then try to query the database with the legacy deprecated functions:

mysql_query($s);

If you are not getting a blattant error message on screen you've probably failed to configure PHP to display error messages in your development box.That's something you need to fix before you go further; it's impossible to code without the aid of error messages. Here's a brief explanation.

Finally, you verify if your connection fails:

if (mysqli_connect_errno($con))

... but you don't test if your query fails.

Community
  • 1
  • 1
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • After i wrote ini_set('display_errors', 1); error_reporting(~0); into code it says Warning: mysql_query() expects parameter 1 to be string, object given in /var/www/database.php on line 29 Warning: mysqli_error() expects exactly 1 parameter, 0 given in /var/www/database.php on line 32 Error inserting data: – user2365812 May 09 '13 at 10:37
  • I'm glad my second and third tips helped you. Now head to the first one ;-) – Álvaro González May 09 '13 at 10:47
0

You seem to be omitting the connection string (using mysql_query instead of mysqli_query) and error verification. Try this:

<?php
// Create connection
$con=mysqli_connect("localhost","******","*******","test");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Mene kotii ku et mitää osaa: " . mysqli_connect_error();
  }

// Insert Data

@$a=$_POST['CustomerId'];
@$b=$_POST['DrinkId'];
@$c=$_POST['SellerId'];
if(@$_POST['submit']) {
    echo $s = "insert into DrinkHistory(CustomerId,DrinkId,SellerId) values('$a','$b','$c')";
    if (mysqli_query($con, $s)) {
        echo "Your Data Inserted";
    } else {
        echo "Error inserting data: ".mysqli_error();
    }
}

?>
Francisco Zarabozo
  • 3,676
  • 2
  • 28
  • 54
  • now it says: "insert into DrinkHistory(CustomerId,DrinkId,SellerId) values('1','3','2')Error inserting data:" – user2365812 May 09 '13 at 10:32
  • Sorry, I forgot to change the function name. I just edited it to correct it. By the way: that code is 100% vulnerable to sql injection. You should always test the data you receive before trying to execute a query with interpolated variables received from a POST. – Francisco Zarabozo May 09 '13 at 10:34