I created a php insert page connected to a phpmyadmin database in xxamp. The page was working fine but when i went to run it today it wouldn't work i think i may have done some thing to the code but not sure.

<?php $connect = mysqli_connect("localhost","root","","ria") or die("Connection error");
?>

php connect

<?php 
if (isset($_POST['required'])) {
	include ('connectmysqli.php');
$title = $_POST['title'];
$cost = $_POST['cost'];
$stock = $_POST['no in stock'];
$insert = "INSERT INTO book (title, cost, no in stock) VALUES ('$title', '$cost', '$stock')";
   if (!mysqli_query($connect, $insert)){
	   die('Cant Insert New Record');
   }
$newrecord = "New record added";

}
?>

insert record

<form action="addbook.php" method="post">

<input type="hidden" name="required" value="true"  />

<fieldset style="width:50px" align="center">

<legend>Insert New Book</legend>

<label>Title:

<input type="text" name="title" /></label>
<br />
<br />
<label>Cost:

<input type="text" name="cost" /></label>
<br />
<br />
<label>Stock:

<input type="text" name="no of stock" />

</label>


<br />
<br />
<input type="submit" value="Submit" />

</fieldset>

</form>

html form

<?php 
echo $newrecord
?>

echo

Does any one have any idea what I have done wrong with this It look me a few days to get working and now it has broken again.

    if you must have a table name with spaces use backticks (`) around it, its better practice to not have spaces in table names.

    also you cant have spaces in your php vars\array keys

      dagon wrote:

      also you cant have spaces in your php vars\array keys

      Actually, that last part isn't true:

      $foo = array('Hello world' => "Foobar");
      ${'Hello World'} = "Foobar";
      
      print_r($GLOBALS);
      

      output:

      Array
      (
          /* ... */
      
      [foo] => Array
          (
              [Hello world] => Foobar
          )
      
      [Hello World] => Foobar
      )

      As for the rest of what dagon said, however, I agree and even offer an extension: normally, you wouldn't want to use spaces in any identifier (e.g. the name of a database, table, column, alias, etc.). Otherwise, you'll have to surround any such references with the appropriate character (e.g. a backtick or a double quote in MySQL, depending upon which mode it's using).

        how about i knew it wasn't true is was still such a good idea i lied about it.😃

          Write a Reply...