0

Im trying to add a a field in to my database but every time i click insert i get the error "Object Not Found" Here is my game_input_form.php

<html><head><title>Games Insert Form</title>
<style type="text/css">
td {font-family: tahoma, arial, verdana; font-size: 10pt }
</style>
</head>
<body>
<table width="300" cellpadding="5" cellspacing="0" border="2">
<tr align="center" valign="top">
<td align="left" colspan="1" rowspan="1" bgcolor="64b1ff">
<h3>Insert Game</h3>

<form method="GET" action="enter_details.php">
Enter Game ID: <input type=text name=gameid size=30><br>
Enter Name: <input type=text name=name size=30><br>
Enter Platform: <input type=text name=platform size=30><br>
Enter Price :<br> <input type=text name=price size=20><br>
<br>
<input type=submit value=Insert><input type=reset>

</form>
</td></tr></table>
</body>
</html>

My gametest.php (I cant find any errors in this file it seems fine)

<?php
    $host="localhost";
    $user="root";
    $password="";
    $con=mysqli_connect($host,$user,$password) or die(mysql_error());
    echo "Connected to MySQL<br/>";
    mysqli_select_db($con,"gamedb") or die(mysqli_error());
    echo "Connected to Database";
    // Create a MySQL table in the selected database
    $query=mysqli_query($con,"CREATE TABLE gameinfo(
                id INT NOT NULL AUTO_INCREMENT,
                PRIMARY KEY(id),
                name VARCHAR(30),
                platform VARCHAR(30),

                price DECIMAL)")
    or die(mysqli_error());  

    echo "Table Created!";

    ?>

Here is my game_display_records.php

<html>
<head><title>Display Records</title>
<style type="text/css">
th, td {font-family: tahoma, arial, verdana; font-size: 10pt; font-weight: 500 }
</style>


</head>
<body>
<?php

 $db="gamedb";
    $link = mysqli_connect('localhost', 'root', '',$db);
    if (mysqli_connect_errno()) {
      trigger_error('Database connection failed: '  . mysqli_connect_error(), E_USER_ERROR);
    }



    $result = mysqli_query($link, "SELECT * FROM gameinfo" ) or die("SELECT Error: ".mysqli_error($link));




    $num_rows = mysqli_num_rows($result);
    print "There are $num_rows records.<br><br>";      
    /* Display a html table    */

    print "<table width=600 border=1>";          
    print "<tr><th>ID</th><th>Name</th><th>Platform</th><th>Price</th></tr>";
    /* Outer loop using mysqli fetch row function to extract a single record and store it in php variable $get_info  */

    while ($get_info = mysqli_fetch_row($result)){
          print "<tr>";

    /* Inner foreach loop  to extract a single field from $get_info and store it in php variable $field  */
          foreach ($get_info as $field) 
               print "<td>$field</td>";    //display the field as a table cell
          print "</tr>";
    }
    print "</table>";
    mysqli_close($link);
    ?>
    <br>

    <form method="POST" action="mainForm.php">
    <input type="submit" value="Database Interface">
    </form>

    </body>
    </html>

and this is my enter_details.php

<html><head><title>Student Insert Record</title></head>
<body>
<?php              

$gameNum=$_GET['gameid'];
$name =$_GET['name'];    
$platform=$_GET['platform'];
$price=$_GET['price'];

$db="gamedb";         

$db = new mysqli('localhost', 'root', '', 'gamedb');
if($db->connect_errno > 0){
    die('Error : ('. $db->connect_errno .') '. $db->connect_error);
}

$result=mysqli_query($db,"INSERT INTO gameinfo (gameid, name, platform, price) 
VALUES ('$gameNum', '$name', '$platform', '$price')") or die("Insert Error: ".mysqli_error($link));
mysqli_close($db);       // Close the connection to the mysql server
print "Record added";
?>

<form method="POST" action="game_input_form.php">
<input type="submit" value="Insert Another game Record">
</form>
<br>

<form method="POST" action="mainForm.php">
<input type="submit" value="Back to Student Records System Menu">
</form>

</body>
</html>

I have went through each page and just cant seem to find the error, i really dont know where I am going wrong, When i go in to myphpadmin in the browser i can see the table with the records but other than that i am lost, any help would be much appreciated? Sorry for the long post

  • your form actions are so misleading tbh – Saad Suri Apr 23 '18 at 11:15
  • Where is `student_insert_record.php` ? – Amit Merchant Apr 23 '18 at 11:15
  • 1
    you should give form action="enter_details.php" – Mohini Apr 23 '18 at 11:23
  • @amitMerchant It should be game_input_form actually but when i insert the record it dosent seem to insert still, i dont get an error but it still dosent work ? – Aaron Smith Apr 23 '18 at 11:25
  • You should change to
    – nacho Apr 23 '18 at 11:28
  • Ok i Have made the edits but now i am getting this error 'Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\Users\Owner\Desktop\College\WIT\Year 3\Semester 2\xampplite\htdocs\games\games\enter_details.php on line 30 Insert Error:' – Aaron Smith Apr 23 '18 at 11:34
  • I have had a look and i am entering in the right details id = 1, name = FIFA, platform = Xbox, price = £39.99 – Aaron Smith Apr 23 '18 at 11:35
  • Your code is vulnerable to [SQL injection](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use [mysqli](https://secure.php.net/manual/en/mysqli.prepare.php) or [PDO](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [this post](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). –  Apr 23 '18 at 11:43
  • Cheers @Dominik ill take a look at that – Aaron Smith Apr 23 '18 at 11:52

1 Answers1

0

In you enter_details.php you have the insert statement.

$result=mysqli_query($db,"INSERT INTO gameinfo (gameid, name, platform, price) 
VALUES ('$gameNum', '$name', '$platform', '$price')") or die("Insert Error: ".mysqli_error($link));

But shouldn't it be

$result=mysqli_query($db,"INSERT INTO gameinfo (id, name, platform, price) 
VALUES ('$gameNum', '$name', '$platform', '$price')") or die("Insert Error: ".mysqli_error($link));

according to your gametest.php you are creating a table with column name id and you are inserting values to a column name gameid.

Hope this resolves it. :)

Rishi Kc
  • 95
  • 9