Jump to content

help!values dont display?


asherinho

Recommended Posts

wats up friends,I am xampp for learning php but recently i faced this problem where the values sent by the script are not display in the database but the rows is added with empty values and the script dont show any error message and the query is succesfull.check the codes

 

 

<?php
$db=mysql_connect("localhost","root");
if(!$db){echo "not connected";}
mysql_select_db("trials",$db);
$query="INSERT INTO organizations VALUES('{$_POST['university']}','{$_POST['colledge']}','{$_POST['institute']}')";
$result=mysql_query($query);
if(!$result){echo "values were not sent".mysql_error();}
  else{echo "values where sent";}
?>

 

EDITED BY akitchin:  code-tag-ified.

Link to comment
Share on other sites

Also you do not need the {} in each of the post variables...

 

wrong:  try running that string definition in PHP without the braces, you'll get a parse error.

 

there's a chance you're using the GET method rather than the POST method.  try echoing your variables before putting them in the query, as this will tell you if the problem is with your query or if it is with your form.  i'm going to say it's with the form, as the !$result condition is proving false.

Link to comment
Share on other sites

You should check whether the variables have values first before using them and never use raw _POST/_GET data in a query.

<?php

// check that the form has been submitted
// normally you'd do this by checking whether the variable
// _POST['submit'] exists which will correspond to the forms submit button
// for this to work make sure you ngive your submit button the name 'submit', eg:
// <input type="submit" name="submit" value="Submit" />
if(isset($_POST['submit']))
{
    $errors = null;

    // check that variables exist first before using the
    if(!isset($_POST['university']) || empty($_POST['university']))
    {
        // if the variable doesn't exist or the variable holds an empty value
        // set an error message
        $error[] = 'Univeristy not supplied';
    }

    if(!isset($_POST['colledge']) || empty($_POST['colledge']))
    {
        $error[] = 'Colledge not supplied';
    }

    if(!isset($_POST['institute']) && empty($_POST['institute']))
    {
        $error[] = 'Institute not supplied';
    }

    // check that the errors variable is not any array
    if(!is_array($errors))
    {
        // error variable is not an array


        // connect to mysql and select database
        $db = mysql_connect('localhost', 'root') or die('not connected');
        mysql_select_db('trials', $db);

        // process submitted data

        // before you use raw _POST data in an query it is recommended to run data strings
        // from the user through mysql_real_escape_string function to pretect against SQL Injection attacks
        $uni = mysql_real_escape_string($_POST['university']);
        $col = mysql_real_escape_string($_POST['colledge']);
        $inst = mysql_real_escape_string($_POST['institute']);

        // setup the query
        $query = "INSERT INTO organizations VALUES('$uni','$col}','$inst')";

        // run the query
        $result = mysql_query($query);

        // check for result
        if($result)
        {
            echo 'Data inserted into database';
        }
        else
        {
            echo 'Error: Unable to add data into database<br />' . mysql_error();
        }
    }
    // if the error variabble is not an array we'll display the errors:
    else
    {
        echo "Please correct the following errors:<br />\n!";
        echo "<ul>\n<li>\n";
        echo implode("</li>\n<li>", $errors);
        echo "</li>\n</ul>\n\n";
    }
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.