TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-17-2009, 03:27 PM   #1 (permalink)
dp2
The Visitor
 
Join Date: Feb 2009
Posts: 4
Thanks: 0
dp2 is on a distinguished road
Default Basic php form update mysql script

I am attempting to build a php form to update my database

I can php read from but trying to put this data into a form that will update my database. Is proving difficult In this example the first part is reading ok I'm now trying to get the data into the form fields, What is happerning is the data and variables are being erased.

Any ideas where I am going wrong? Thank you

Code:
<HTML>
<HEAD>
<TITLE> The Reg Directory </TITLE>
<HEAD>
<BODY>
<?php 

$dbcnx = @mysql_connect("localhost", "username", "password");
if (!$dbcnx) {
  echo( "<P>Unable to connect to the" .
        "database server at this time.</P>" );
  exit();
}

//select which database you want to edit
mysql_select_db("aynsley_phpbb1",$dbcnx);

if (! @mysql_select_db("aynsley_phpbb1") ) {
  echo( "<P>Unable to locate the regdir " .
        "database at this time.</P>" );
  exit();
}

 // Request the text of all the business names
  $result = mysql_query(
            "SELECT BusinessName FROM regdir");
  if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  }
//mysql_query(<query>, <connection id>);
while ($row=mysql_fetch_array($result)) {
  echo("<P>" . $row["BusinessName"] . "</P>");
}
?>
<P> Here are all the Businesses in our database: </P>
<BLOCKQUOTE>

<?php

 // Request the text of all the business names
  $result = mysql_query(
            "SELECT BusinessStreet FROM regdir");
  if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  }

  // Display the text of each BusinessName in a paragraph
  while ( $row = mysql_fetch_array($result) ) {
  }
?>
<html>
<body>
<form method="post"action="<?php echo $PHP_SELF ?>">
<input type=text name="action" value="">
Business Name 
<input type=text name="BusinessName" value="<?PHP echo $BusinessName ?>"></br>
<input type=text name="BusinessStreet" value="<?PHP echo $BusinessStreet ?>">
<input type=submit name="submit" value="submit">
</form>
<?php
  echo("$BusinessStreet");
  

?>
</BLOCKQUOTE>
</BODY>
</HTML>
dp2 is offline  
Reply With Quote
Old 02-17-2009, 07:06 PM   #2 (permalink)
The Contributor
 
Join Date: Feb 2009
Posts: 65
Thanks: 0
Krik is on a distinguished road
Default

Well you have not defined "$BusinessName" or "$BusinessStreet". If you leave the code unchanged you would need to use $row[0], $row[1], ect.

Or you could use "list" or "foreach" to make variables with those names.

PHP Code:
while ( $row mysql_fetch_array($result) ) {
list (
$BusinessName$BusinessStreet) = $row;

PHP Code:
while ( $row mysql_fetch_array($result) ) {
    foreach (
$row as $key => $value) {
    }

Also you need to have the data echoed inside the while or foreach loops. Else you will only get the last record that was returned.
PHP Code:
<?php
while ( $row mysql_fetch_array($result) ) {
    foreach (
$row as $key => $value) {
?>
    <input type=text name="BusinessName" value="<?PHP echo $BusinessName ?>"></br>
    <input type=text name="BusinessStreet" value="<?PHP echo $BusinessStreet ?>">
<?php
    
}
}
?>
Krik is offline  
Reply With Quote
Old 02-18-2009, 06:48 AM   #3 (permalink)
dp2
The Visitor
 
Join Date: Feb 2009
Posts: 4
Thanks: 0
dp2 is on a distinguished road
Default

Thank you for all your help.

This is what I have achieved with your suggestions.

It's nearly there BusinessName displays BusinessStreet 1 result and BusinessStreet displays BusinessName.

Next stage will it update?

Code:
<HTML>
<HEAD>
<TITLE> The Edit Directory Listing</TITLE>
<HEAD>
<BODY>
<?php 

$dbcnx = @mysql_connect("localhost", "username", "password");
if (!$dbcnx) {
  echo( "<P>Unable to connect to the" .
        "database server at this time.</P>" );
  exit();
}

//select which database you want to edit
mysql_select_db("aynsley_phpbb1",$dbcnx);

if (! @mysql_select_db("aynsley_phpbb1") ) {
  echo( "<P>Unable to locate the regdir " .
        "database at this time.</P>" );
  exit();
}

 // Request the text of all the business names
  $result = mysql_query(
            "SELECT BusinessName FROM regdir");
  if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  }
//mysql_query(<query>, <connection id>);
while ($row=mysql_fetch_array($result)) {
list ($BusinessStreet, $BusinessName) = $row;
}
 // Request the text of all the business names
  $result = mysql_query("SELECT * FROM regdir");
  if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  }

  // Display the text of each BusinessName in a paragraph
 while ( $row = mysql_fetch_array($result) ) {
list ($BusinessName, $BusinessStreet) = $row;
}  
?>
<html>
<body>
<form method="post"action="<?php echo $PHP_SELF ?>">
Business Name:
<input type=text name="BusinessName" value="<?PHP echo $BusinessName ?>"><br>
Business Street:
<input type=text name="BusinessStreet" value="<?PHP echo $BusinessStreet ?>"><br>

<input type=submit name="submit" value="submit">
</form>

</BLOCKQUOTE>
</BODY>
</HTML>
I have two queries going on one is not working no errors
Is there a simple method to read a complete row from 1 ID field
dp2 is offline  
Reply With Quote
Old 03-11-2009, 04:06 PM   #4 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

Quote:
Originally Posted by dp2 View Post
I have two queries going on one is not working no errors
Is there a simple method to read a complete row from 1 ID field
you can read 1 row based on a value like this:

PHP Code:
$result mysql_query("SELECT * FROM regdir WHERE BusinessID=".$id); 
in the query above the * would retrieve all the fields in that row. And the &id would have to be a variable holding the id of the row you want to retrieve.

P.D. you might want to restructure your code so it could be more legible, because I can't tell but maybe there is something wrong with the way the form is display, because I don't think you need 2 queries for this.
Just an advice.

Last edited by tony : 03-11-2009 at 04:08 PM. Reason: write advice
tony is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Contact Form Script Matt83 Script Giveaway 20 10-22-2012 10:18 AM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
email fill in form help? PHP sacred_tinker Absolute Beginners 19 05-28-2008 05:58 AM
Error in connecting to MySQL via PHP EyeDentify MySQL & Databases 0 01-03-2008 01:06 PM
Uploading Files with PHP daz Absolute Beginners 3 09-30-2007 06:23 PM


All times are GMT. The time now is 02:21 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design