PHP MYSQLI INSERT PROBLEM

Alles, was PHP betrifft, kann hier besprochen werden.

PHP MYSQLI INSERT PROBLEM

Postby lunaticbit » 15. January 2016 10:51

I cannot understand why I receive the message "Parse error: syntax error, unexpected 'INTO' (T_STRING) in C:\xampp\htdocs\nic\receive_values.php on line 33"
running the second module which contains the INSERT statement. The modules are:

MODULE 1:

Code: Select all

<html>
<head>
   <title>
      Insert Values
   </title>
   <style>
      @import url("nic.css");
   </style>
</head>
<body>
<form action="receive_values.php" method="POST">
    F0: <input type="text" name=onoma[0]>
      <input type="text" name=typos[0]><br>
    F1: <input type="text" name=onoma[1]>
      <input type="text" name=typos[1]><br>
    F2: <input type="text" name=onoma[2]>
      <input type="text" name=typos[2]><br>
    F3: <input type="text" name=onoma[3]>
      <input type="text" name=typos[3]><br>
    F4: <input type="text" name=onoma[4]>
      <input type="text" name=typos[4]><br>   
   
    <input type="submit">
</form>
</body>
</html>



MODULE 2

Code: Select all

<html>
<head>
   <title>
      Receive Values
   </title>
   <style>
      @import url("nic.css");
   </style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$db = new mysqli($servername, $username, $password, $dbname);

if ($db->connect_errno)
{
   echo $db->connect_errno;
   die('Connection Problem!');
}
//==================================
   $onoma   = $_POST['onoma'];
   $epwnymo = $_POST['typos'];
   $j = 0;
   for ($i = 0; $i < count($onoma); $i++)
   {
      if ($name[$i] != "")
      {
         $j += 1;
         echo $j, ' : ', $onoma[$i], ' ... ', $typos[$i], '<br>';
         INSERT INTO nic(onoma, typos) VALUES('$onoma[$i]', '$typos[$i]');
      }
   }
   echo '<hr>Total Records Inserted: ', $db->affected_rows;

 ?>
</body>
</html>

lunaticbit
 
Posts: 21
Joined: 09. April 2015 16:24
Operating System: Windows 7

Re: PHP MYSQLI INSERT PROBLEM

Postby Nobbie » 15. January 2016 12:44

Because this is invalid code:

Code: Select all
   for ($i = 0; $i < count($onoma); $i++)
   {
      if ($name[$i] != "")
      {
         $j += 1;
         echo $j, ' : ', $onoma[$i], ' ... ', $typos[$i], '<br>';
         INSERT INTO nic(onoma, typos) VALUES('$onoma[$i]', '$typos[$i]');
      }
   }


This "INSERT INTO ..." line is horribly wrong there! PHP does not know SQL Statements, you cannot simply put a SQL statement in your PHP Code. Did you ever read a tutorial or some documentation about PHP / MySQL? In your title you mention an "MYSQLI" problem, you should read the documentation about mysqli (either the functions or the class). Basically, you have to create a PHP string (with quotes), which holds the SQL INSERT statement. And then you have to pass this string to a mysqli_query() function (or to the correspondig mysqli->query method).

But you CANNOT simply put this SQL statement plain into your PHP code. Have you ever seen that in another example?

Here is a link to the original PHP documentation, read bout mysqli_query: http://php.net/manual/en/mysqli.query.php

There are also some examples how to deal with MySQL Databases (using mysqli).
Nobbie
 
Posts: 8775
Joined: 09. March 2008 13:04


Return to PHP

Who is online

Users browsing this forum: No registered users and 1 guest