7 Replies - 725 Views - Last Post: 26 April 2010 - 02:48 AM Rate Topic: -----

#1 gtrippleb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 03-November 09

Need help inserting values into MySQL Database

Posted 25 April 2010 - 02:44 AM

I'm trying to code a page that when someone enters in some text and clicks submit, that text gets inserted into a database and is assigned a number which is auto incremented. When I run a query from a php page to view all of the results in the table, I can see that a row is being created, but there are no values being inserted. Another issue that I'm having is that when I try and display the number that is generated from the ID column on the submission page, the ID number sisn't showing. Right now I have 2 pages coded that allows me to enter in the text and then a php page that inserts the data into table. I also created another php page to view all of the data in the table which I'm not having any problems with at the moment. The information on the table is attached.

This is the code for the page to enter in the text:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>New Bug Report</title>
<link rel="stylesheet" href="php_styles.css" type="text/css" />
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>

<h1>New Bug Report</h1>
<form action="createbug3.php" method="post">
	<p>Short Description of Bug:<br /><input type="text" name="sDesc" size="65" /></p>
    <p>Long Description of Bug:<br /><textarea name="lDesc" rows="4" cols="50"></textarea></p>
    <p>Actions Taken to Rectify/Diagnose Bug:<br /><textarea name="act" rows="4" cols="50"></textarea></p>
    <p><input type="reset" value="Reset" /> <input type="submit" value="Submit Bug" /><br />
    <input type="hidden" name="submitted" value="TRUE" /></p>
</form>
<p><a href="BugReport.html">Return to Bug Report Home</a></p>
</body>
</html>




Here is the code to insert the text entered and then display the assigned Id number.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="php_styles.css" type="text/css" />
</head>

<body>


<?php
$DBConnect = @mysqli_connect("serverinfo", "username", "password")
	Or die("<p>Unable to select the database</p>" . "<p>Error code " . mysqli_connect_errno()
	. ": " . mysqli_connect_error()) . "</p>";	
$DBName = "bugs";
@mysqli_select_db($DBConnect, $DBName)
	Or die("<p>Unable to select the database.</p>" . "<p>Error code " . mysqli_errno($DBConnect)
	. ": " . mysqli_error($DBConnect)) . "</p>";
	
$TableName = "reports";
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);
	
$shortdesc = addslashes($_GET['sDesc']);
$longdesc = addslashes($_GET['lDesc']);
$actions = addslashes($_GET['act']);

$SQLstring = "INSERT INTO reports VALUES(NULL,'$shortdesc', '$longdesc', '$actions')";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
	Or die("<p>Unable to execute query.</p>" . "<p>Error code " . mysqli_errno($DBConnect)
	. ": " . mysqli_error($DBConnect)) . "</p>";
	
$BugID = mysqli_insert_id($DBConnect);
mysqli_close($DBConnect);



?>

<p>You have succesfully submitted a new bug entry. This bug has been assigned the ID:<? $BugID ?></p> 
<form action="buglistings.php" method="get">
<p><input type="submit" value="Enter New Bug" />
<input type="hidden" name="id" value="<?= $BugID ?>" /></p>
</form>

</body>
</html>





Any help is greatly appreciated.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Need help inserting values into MySQL Database

#2 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Need help inserting values into MySQL Database

Posted 25 April 2010 - 03:27 AM

There are some areas you have done mistake. First, you have used post method in the form, but while retrieving form data, you have used get method, thats why nothing is being inserted into the database. So replace these lines with $_POST method-

$shortdesc = addslashes($_GET['sDesc']);
$longdesc = addslashes($_GET['lDesc']);
$actions = addslashes($_GET['act']);



The other issue with the id of each entry you are entering to database is not showing because you are not echoing it-

This bug has been assigned the ID:<? $BugID ?>



use

This bug has been assigned the ID:<?= $BugID ?>



Hope it will help you.
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: Need help inserting values into MySQL Database

Posted 25 April 2010 - 05:41 AM

Note that you can only use this:
This bug has been assigned the ID:<?= $BugID ?>

if short_tags are permitted in the php.ini file. For better compatibility use the full form:
This bug has been assigned the ID:<?php echo $BugID ?>

Was This Post Helpful? 3
  • +
  • -

#4 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Need help inserting values into MySQL Database

Posted 25 April 2010 - 06:05 AM

As he has used the <?= ?> delimiter in another line of the script, i assumed that short_tags are enabled in php.ini file.

<input type="hidden" name="id" value="<?= $BugID ?>" />



If its not enabled, you must use standard delimiter <?php ?> as mentioned by JackOfAllTrades in the previous post or you need to enable short_tags in php.ini file.
Was This Post Helpful? 1
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: Need help inserting values into MySQL Database

Posted 25 April 2010 - 08:15 AM

I missed that, so good eye. Short tags are off by default (and maybe going away?) so it's something to keep in mind, particularly for future consideration.
Was This Post Helpful? 0
  • +
  • -

#6 gtrippleb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 03-November 09

Re: Need help inserting values into MySQL Database

Posted 25 April 2010 - 08:39 AM

I'm not sure if short tags are turned on, so I'll use
<?php ?>
and see what happens Thanks for the suggestions. The book I was using for an example had the
<?= variable ?>
. I didn't even think of short tags being turned on or off.

This post has been edited by gtrippleb: 25 April 2010 - 08:43 AM

Was This Post Helpful? 0
  • +
  • -

#7 gtrippleb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 03-November 09

Re: Need help inserting values into MySQL Database

Posted 25 April 2010 - 09:02 AM

I changed the _GET to _POST and I can now see the values entered.

Once I changed my code to show the bug id from
<?= $BugID ?> to <?php echo $BugID ?>

I get a number but the number is 0 instead of 461, which is the current number in the id column.

It looks like it matters where the
$BugID = mysqli_insert_id($DBConnect);
is placed, a suggestion from another place, as I moved it up below my database connection string instead of above the database close string where I had it and that's when I got the 0. I since moved it back and now I get the number that's in the id column.

Thanks for the help in getting this to work.

This post has been edited by gtrippleb: 25 April 2010 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#8 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Need help inserting values into MySQL Database

Posted 26 April 2010 - 02:48 AM

View Postgtrippleb, on 25 April 2010 - 08:39 PM, said:

The book I was using for an example had the
<?= variable ?>
. I didn't even think of short tags being turned on or off.


<?= $var ?> is a variation of short tags and is known as short circuit syntax which is generally used for echoing something. As it turned off by default, for maximum portability option, you need to use standard tags <?php ?>.

This post has been edited by atik97: 26 April 2010 - 02:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1