Forum Moderators: coopster

Message Too Old, No Replies

Pulling out hair...

         

Bigjohn

8:12 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



I'm trying to make a function:

function make_page_lable($artist) {
$sql = $sql = 'SELECT *'
. 'FROM artists'
. 'WHERE artist LIKE "%'. $artist .'%"';
$result = mysql_query($sql);
if (!$result) {
print mysql_error() . "Error - discriptor Query Failed";
}
$content="";
$row .= mysql_query($result);
$content = '<h1 align="center">' . $row['artist'] . '</h1>';
$content .= '<p align="center">'. $row['artistbio'].'</p>';
return $content;
}

I'm using LIKE because the field in the DB is likely to have upper case letters - and to use the same value throughout the page I strlower($artist).

I've tried all sorts of variations on the punctuation. I'm sure its something simple, but my newbie eyes are missing it... Please help?

John

Timotheos

9:58 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That link is to the official PHP manual which will be your greatest friend, besides me of course ;-).

I wouldn't use that script unless you want to send attachments. Just use the mail function that comes with php. It's really quite simple. Go here in the manual [php.net] to get more of the specifics.

Bigjohn

4:45 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



I like the little foreach loop:

foreach ($_POST as $field) {
echo "<p>",$field;
}

Question:
How can i write a loop like this that will output the field names too?

Timotheos

5:13 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I like 'em too

foreach ($_POST as $fieldname => $value) {
echo "$fieldname is $value<br />";
}

Bigjohn

5:16 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Will that echo blank field names too?

Thanks!

EDIT: DOH >tryit and see< DOH

Bigjohn

11:37 pm on Mar 1, 2004 (gmt 0)

10+ Year Member



I'm thinking that this is the correct syntax for the UPDATE - but how do I code it into the PHP?


$sql = 'UPDATE '. $class . 'SET'.$itemstatus="0".'WHERE item ='.$itemno;

Timotheos

12:32 am on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks good Bigjohn... just watch your spacing and I think you'll want to fix the SET part like below because you're referencing the database column and not a php variable:

$sql = 'UPDATE '. $class . ' SET itemstatus=0 WHERE item ='.$itemno;

Now just run it with mysql_query($sql);

Bigjohn

1:08 am on Mar 2, 2004 (gmt 0)

10+ Year Member



Thanks! I'll give that a try. I just couldnt figure if it was mysql_query or if there was a special command for updating... because there is special command for fetch_array, etc...

Thanks!

John

Bigjohn

1:22 am on Mar 2, 2004 (gmt 0)

10+ Year Member



if I did a DB connect a page earlier, do I have to do it again?

Bigjohn

1:43 am on Mar 2, 2004 (gmt 0)

10+ Year Member



ARRGH.

Just when I think I have a handle on it...

What is wrong with this code?


/* get variables */
$cname = $_POST['fullname'];
$ccompany = $_POST['company'];
$caddr1 = $_POST['address1'];
$caddr2 = $_POST['address2'];
$ccity = $_POST['city'];
$cstate = $_POST['state'];
$ccountry = $_POST['country'];
$isbill = $_POST['isbilladdress'];
$baddr1 = $_POST['billaddress1'];
$baddr2 = $_POST['billaddress2'];
$bcity = $_POST['billcity'];
$bstate = $_POST['billstate'];
$bzip = $_POST['billzip'];
$bctry = $_POST['billcountry'];
$cphone = $_POST['telephone'];
$cfax = $_POST['fax'];
$cemail = $_POST['email'];
$payby = $_POST['paymentmethod'];
$instruct = $_POST['specialinstructions'];
$termsok = $_POST['acceptterms'];
$maillist = $_POST['maillist'];
$itemdesc = $_POST['itemdesc'];
$price = $_POST['price'];
$itemno = $_POST['itemno'];
$class = $_POST['table'];
/* now build functions */

function make_confirm (){
echo '<h2 align="center">Your order is Confirmed</h2>';
if ($isbill!= 'yes') {echo '<p> Your billing and shipping addresses are different.
These addresses must be confirmed before shipment will be made.</p>';}
echo '<p>Pay Pal purchases will only be shipped to a confirmed Pay Pal address. </p>';
if ($ccountry!= 'United States') {echo '<p>Your international shipping options will be sent to you in the followup email.</p>';}
echo '<p>Your order for '.$itemdesc . 'priced at $'.$price .' has been submitted. You will receive a confirmation email shortly.</p>';
echo '<p>Within 36 hours you should also receive email from us regarding shipping and confirming your payment method.</p>';
echo '<p>Thank you for your purchase! We look forward to serving your needs in the future.</p>';
$sql = 'UPDATE '. $class . 'SET itemstatus=0 WHERE item ='.$itemno;
mysql_query($sql);
}
print make_confirm();

When I run the loop

foreach ($_POST as $fieldname => $value) { 
echo "$fieldname is $value<br />";
}

it echos the fields... but in the code above, none of the variables show up!

John

willybfriendly

2:55 am on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't have an answer to your question, but...

I would suggest that you do not draw the price from the $_POST array, or that if you do that you should compare to the price in the DB.

Failure to do this opens the door to someone calling the script with altered form fields and obtaining the item for far less than you are selling it for!

WBF

Timotheos

6:01 am on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it echos the fields... but in the code above, none of the variables show up!

Are you sure you're posting it correctly? No variables indicates the form is not working right. You can give us a snippet of your post form if you like.

Bigjohn

1:42 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



@ Timotheos

When I run the function with your 'foreach' loop (above) it echos the content of the _post array!


<h2 align="center">Order Form</h2>
<form name="orderform" action="postorder.php" method="post" onSubmit="return fcheckfields(document.forms[0])">
<?php
echo '<input name="table" type="hidden" value="'.$table.'">'."\n";
echo '<input name="itemno" type="hidden" value="'.$itemnum.'">'."\n";
echo '<input name="itemdesc" type="hidden" value="'.$itemdesc.'">'."\n";
echo '<input name="price" type="hidden" value="'.$price.'">'."\n";
?>
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td>
<div align="right">
<b>Full name:</b></div>
</td>
<td width="6"></td>
<td><input type="text" name="fullname" size="50"></td>
</tr>
<tr>
<td>
<div align="right">
<b>Company:</b></div>
</td>
<td width="6"></td>
<td><input type="text" name="company" size="50"></td>
</tr>
<tr>
<td></td>
<td width="6"></td>
<td><b>Shipping address:</b></td>
</tr>
<tr>
<td>
<div align="right">
<b>Address Line 1:</b></div>
</td>
<td width="6"></td>
<td><input type="text" name="address1" size="50"></td>
</tr>
<tr>
<td>
<div align="right">
<b>Address Line 2:</b></div>
</td>
<td width="6"></td>
<td><input type="text" name="address2" size="50"></td>
</tr>
<tr>
<td>
<div align="right">
<b>City:</b></div>
</td>
<td width="6"></td>
<td><input type="text" name="city" size="30"></td>
</tr>
<tr>
<td>
<div align="right">
<b>State/Province/Region:</b></div>
</td>
<td width="6"></td>
<td><input type="text" name="state" size="30"></td>
</tr>
<tr>
<td>

Thats a snip of the form.

Bigjohn

1:55 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



Am i assinging the post variable correctly?

Is there a punctuation error somewhere in my other code where I'm trying to replace variables?

John

coopster

2:11 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The variables are local to the function (they don't *exist* within the function). They either have to be passed or defined within the scope of the function.

If you were to simply move your /*get variables*/ chunk of code into the top of your makeconfirm function, you will get the desired results.

Also, you are using print makeconfirm(), but echoing the data within the function. You may want to remove the print function and just call your user-defined function.

Bigjohn

2:24 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



how do you just 'call' the function?

coopster

2:39 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



To call a function [php.net]:
make_confirm();
all by its lonesome.

By the way, here's a link regarding variable scope [php.net].

Bigjohn

2:49 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



Ok. That works, but my SQL update does not seem to be working. Is there a way to test what happens?


$sql = 'UPDATE '. $class . 'SET itemstatus=0 WHERE item ='.$itemno;
mysql_query($sql);

It's not returning an error, but when I go back through the database it's not been set to -0-

John

coopster

3:02 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There is also a tip in the PHP Troubleshooting [webmasterworld.com] thread in the PHP Library [webmasterworld.com] that explains this:
$sql = 'UPDATE '. $class . 'SET itemstatus=0 WHERE item ='.$itemno; 
exit($sql);
mysql_query($sql);

Netizen

3:06 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



Have you tried echo'ing the SQL statement? Alternatively, if there is a MySQL error then mysql_errno() will return non-zero and mysql_error() [php.net] will give the error message.

Example code snippet:

if (mysql_errno()) {
printf("Error is %s",mysql_error());
}

Bigjohn

3:15 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



Hey that's cool!

Here's what comes out:


UPDATE ornith SET itemstatus=0 WHERE item =110001

do I have to have the DB name there too? Here is the code for the SQL:


$dbh=mysql_connect ("localhost", "****xx_joetest", "****xx") or die ('Cannot connect to Database - error: ' . mysql_error () );
mysql_select_db("****xx_joetest1",$dbh);
$sql = 'UPDATE '. $class . ' SET itemstatus=0 WHERE item ='.$itemno;
exit($sql);
mysql_query($sql);

Do I need a space between the "item ='" - such as "item = '"?
Or do I need to be more specific in my SQL statement, such as specifying the DBname and table? (xxxjoetest1.$class)<i know thats wrong syntax.. just conveying the idea...>

when I run the query replacing the variable with fixed values in myPHPadmin the query works...

The query on the page returns no errors...
John

Bigjohn

5:20 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



Do I need to close and open database sessions? I'm back to the hair pulling-out again. The UPDATE query looks ok to me, and it does in fact run when I use fixed values...I've tested that in myphpadmin.

Timotheos

5:47 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm beginning to pull out my hair too! ;-)

Remember that the exit($sql) terminates your script. It is simply a debugging tool. In this circumstance I usually just echo $sql so I get a look at the sql and let it run to see if it works or if I get any errors.

I can't see anything wrong.

Bigjohn

5:51 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



OH! I did not know that EXIT would terminate the query without executing it.

Let me remark that out now that I have the spacing correct. Stay Tuned!

John

Update: (no pun!) - Dang! See, it was just spacing and such. Once I got the SQL right I should have taken out the EXIT. Dang! Now it's working DANDY!

Thanks a million guys!

Next step - pass all these variables to the administrator via email for processing...

John

Bigjohn

10:38 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



On to the next bit!

I'm using the MAIL() function. The mail it generates says it's from NOBODY @ myhostingcompay server....

How can I set the FROM?

John

coopster

12:32 am on Mar 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can set it in your headers [php.net] and/or you can set it in your php.ini [php.net] file, Bigjohn.

Bigjohn

12:41 am on Mar 3, 2004 (gmt 0)

10+ Year Member



Thats what I thought - re: the headers...
how do you set up a PHP.INI file for an individual user on virtual hosting?

John

coopster

12:53 am on Mar 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Typically you can't -- hosting providers won't allow it, for the obvious reasons -- there has to be some continuity and control for everyone sharing a server. However, you can override some of the configuration directives with techniques such as per-dirctory configuration overrides [php.net] (.htaccess files) or ini_set [php.net].

Sooner or later, you're going to want to know all about PHP configuration [php.net]!

Bigjohn

1:19 am on Mar 3, 2004 (gmt 0)

10+ Year Member



Ok... that configuration link blew the back off my brain-pan.

Umm, Ok, so I'll try the headers thing!

Thanks

One more question!

When I insert a row, how can I return the 'autoincrement ID' value?

John

Timotheos

1:48 am on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok... that configuration link blew the back off my brain-pan.

Well it's no wonder since your hair folicles (which have gone missing) were actually attaching that to your head.

Umm, Ok, so I'll try the headers thing!

$headers = "From: " . $first_name . " " . $last_name . " <" . $email . ">\r\n";
mail($to, $subject, $message, $headers);

When I insert a row, how can I return the 'autoincrement ID' value?

$id = mysql_insert_id();

Bigjohn

2:27 am on Mar 3, 2004 (gmt 0)

10+ Year Member



Do I do that AFTER the insert?

John

This 62 message thread spans 3 pages: 62