Jump to content

Cant Insert Values In Database, Wrong Code!


sid0972

Recommended Posts

hey, newbie here

 

i am writing a code to insert some values in database, it actually worked in the afternoon, but when i started the system in the night it magically stopped working.

 

First, i am taking the value in a text box, then forwarding it to a file to be stored in a variable, then calling a function (which is in yet another file) which stores the values in the database.

 

the code is here

 

text box

<form action="add.php" method="post">
<center><input type="text" name="abc" size="9" maxlength="9">
<br><br>
<input type="submit"  value="Save">

 

to hold "abc" in a variable

 

$abc=$_POST['abc'];

add_abc($abc);

 

to insert it in a database

 

<?php
session_start();


require_once('db_fns.php');


function add_abc($abc)
{
$valid_user = $_SESSION['valid_user'];


 $conn = db_connect();
 if ($result && ($result->num_rows>0)) {
   throw new Exception('already exists.');
 }

  if (!$conn->query("insert into scores (username,abc) values
    ('".$valid_user."', '".$abc."')")) {
   throw new Exception('could not be inserted.');
 }
else { echo "successful";}
 return true;

 

 

can anyone please tell me whats wrong here??

and these are the error i get on file abc.php

 

PHP Notice: Undefined index: abc in /var/www/work/fns/add_oc_middle.php on line 5
PHP Notice: Undefined index: valid_user in /var/www/work/fns/add.php on line 13
PHP Notice: Undefined variable: result in /var/www/work/fns/add_oc_middle.php on line 16
PHP Fatal error: Uncaught exception 'Exception' with message 'Score could not be inserted.' in /var/www/work/fns/add_oc_middle.php:22
Stack trace:
#0 /var/www/work/fns/add_oc_middle.php(7): add_abc(NULL)
#1 {main}
thrown in /var/www/work/fns/add.php on line 22


Sounds like the form isn't being posted. You need to do some debugging on your own, especially since you have only shown us snippets of the code. Walk through it and check the values at every step you can find, until you find it doesn't match your assumptions.

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.