Issue with my edit code...

Hi Guys,

I’m trying to do the following:

[ul][li]User inputs an ID in a search box[/li]
[li]Query goes off to the DB, finds the record[/li]
[li]Returned results are populated into fields in a form[/li]
[li]User edits the fields and submits the new values[/li]
[li]Submitting script updates the required table with the new values[/li][/ul]

Now, I haven’t completed my code fully as yet. However, I wanted to test out some stuff before continuing and now I’m getting an error and I’m not sure why. Basically, I’m checking if I can grab the required values before I build the form. But it’s not working. Here’s my code:

[php]

<? mysql_connect("localhost", "******", "******") or die(mysql_error()); mysql_select_db("******") or die(mysql_error()); //Register globals are set to off, so we need to get the following across $animalID = $_POST['animalID']; //Now we have connected, we're going to match the details that are entered into the Animal ID field $sql = "SELECT 'animalID', 'status', 'date', 'species', 'breed', 'sex', 'primary_colour', 'colour', 'distinctive_traits', 'fur_length', 'age', 'desexed', 'microchipped', 'suburb', 'pound_area', 'contact', 'link' FROM animal_info WHERE 'animalID' = '%$animalID%'"; $result = mysql_query ($sql) or die(mysql_error()); $query=getenv(QUERY_STRING); parse_str($query); ?>

[/php]

<html>
<body>
<title>Edit an Animal</title>
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<div id="art-page-background-glare-wrapper">
<div id="art-page-background-glare">
</div>
</div>
<div id="art-main">
<div class="cleared reset-box">
</div>
<div class="art-box art-sheet">
<div class="art-box-body art-sheet-body">
<div class="art-header">
<div class="art-headerobject">
</div>
<div class="art-logo">
<h1 class="art-logo-name">
<a href="index.php">Pets of Perth</a>
</h1>
<h2 class="art-logo-text">Lost and Found</h2></div>
</div>
<div class="cleared reset-box">
</div>
<div class="art-layout-wrapper">
<div class="art-content-layout">
<div class="art-content-layout-row">
<div class="art-layout-cell art-sidebar1">
<div class="art-box art-vmenublock">
<div class="art-box-body art-vmenublock-body">
<div class="art-bar art-vmenublockheader">
<h3 class="t">Main Menu</h3>
</div>
<div class="art-box art-vmenublockcontent">
<div class="art-box-body art-vmenublockcontent-body">
<ul class="art-vmenu">
<li>
<a href="index.php" class="">Welcome!</a>
</li>
<li><a href="add.php">Add an Animal</a></li>
<li> <a href="search.php">Search for an Animal</a>
<li> <a href="edit.php" class="active">Edit an Animal</a>
</li>
</ul>
</li>	
</ul>
<div class="cleared"></div>
</div>
</div>
<div class="cleared">
</div>
</div>
</div>
<div class="cleared">
</div>
</div>
<div class="art-layout-cell art-content">
<div class="art-box art-post">
<div class="art-box-body art-post-body">
<div class="art-post-inner art-article">
<h2 class="art-postheader">Editing <?php echo $animalID;?></h2>
<div class="art-postcontent">
<br />
<br />

[php]

<?php while ($row = $sql->fetch_assoc()); echo $row['primary_colour']; ?>

[/php]

<BR></p> 
  </div>
	<div class="cleared">
</div>
</div>
<div class="cleared"></div>
</div>
</div>
<div class="cleared"></div>
</div>
</div>
</div>
</div>
<div class="cleared">
</div>
<div class="art-footer">
<div class="art-footer-body">
<a href="#" class="art-rss-tag-icon" title="RSS"></a>
<div class="art-footer-text">
<p>Copyright &copy; 2012 Pets of Perth, Lost and Found. All Rights Reserved.</p>
<p>Website by <a href="#" target="_blank">Company Name</a>
</p>
</div>
<div class="cleared"></div>
</div>
</div>
<div class="cleared"></div>
</div>
</div>
<div class="cleared"></div>
<p class="art-page-footer"></p>
<div class="cleared"></div>
</div>
</body>
</html>

I’m getting the error:

Fatal error: Call to a member function fetch_assoc() on a non-object in home/loveani/public_html/poplaf/edit_process.php on line 93

Any help would be appreciated!

Cheers,

Dave

Well, not really sure what you showed us… Basically, PHP is usually called by a FORM which is POSTED to another file that is the PHP code. Then, the PHP code creates an a results based on the database info it pulls and displays a new page or loads another… You should a bunch of HTML, some PHP, some HTML, nothing that would ever call the PHP code… Are we missing something…

So, normally, you create an HTML page. Inside of this page, you have a FORM. Inside of the FORM, you have fields such as drop-downs or text fields and these have names assigned to them. Then, inside the PHP code that is called from the FORM, you can read the results of the FORM, in other words the FIELD values and use them in whatever manner you need to.

But, you did not show any FORM code and therefore, the PHP code can not be debugged and actually will never run as it is never called… Help us help you… Post your FORM code…

Yeah, I was just posting as per the rules of the forum by encasing my PHP parts in the PHP code and other code in the normal code tags.

If you put everything together, you get my code!

Basically, I know how to construct a form and post the results to the DB, but at the moment, if you put everything together, I’m not getting any results from my DB.

I’m just checking to see if I can echo a result from the table before I go any further.

So, I’m searching for a record in my DB via a form that’s posting to this code. The code then querys the DB and prints a value matching the record.

That’s basically all i’m trying to achieve at this point.

Okay, I will piece it together and put it in my editor and look at it. Usually, it is easiest for people reading it to have each file inside one tag. If the file contains php, put it in php tags. If not, put it in # tags. If there are two files put each in separate ones. Adding the file name just before the first tag helps too…

I will look at it now…

Well, a quick look show no form info again. Nothing jump out as incorrect except the original Query.
This line does not need quotes around the field names. Change this:
$sql = “SELECT ‘animalID’, ‘status’, ‘date’, ‘species’, ‘breed’, ‘sex’, ‘primary_colour’, ‘colour’, ‘distinctive_traits’, ‘fur_length’, ‘age’, ‘desexed’, ‘microchipped’, ‘suburb’, ‘pound_area’, ‘contact’, ‘link’ FROM animal_info WHERE ‘animalID’ = ‘%$animalID%’”;

to this:

$sql = "SELECT animalID, status, date, species, breed, sex, primary_colour, colour, distinctive_traits, fur_length, age, desexed, microchipped, suburb, pound_area, contact, link  FROM animal_info WHERE animalID = '" . $animalID . "'";

Also, in the page that calls code, make sure the animalID is spelled the same.

Lastly, why would you want to set the variables to use the external server’s environmental page query string? I do not understand how that would help you in any way. It basically has no use. It just slows up your code in many ways. The only thing that I can come up with for using those two line would be if you wanted to have several frames and pull the query from a different page. But, this would be better done with session variables. Not sure why that would be in your code. Can you explain that? Thanks.

Sponsor our Newsletter | Privacy Policy | Terms of Service