Jump to content

Recommended Posts

???

 

$query_temp_tbl = "CREATE TEMPORARY TABLE temp_tbl SELECT * FROM reports WHERE report_name = ('$selected_report')";
mysql_query($query_temp_tbl);
$query_req_fields = "SELECT required FROM temp_tbl";
$result_req_fields = mysql_query($query_req_fields,$link);
$query_field_desc_i = "SELECT field_desc_i FROM temp_tbl";
$result_field_desc_i = mysql_query($query_field_desc_i,$link);
  
for ($cntr = 0; $cntr < $num_rows; $cntr += 1) {
  if (mysql_result($result_req_fields, $cntr) == '1') {
   if ($_POST(mysql_result($result_field_desc_i, $cntr)) == '') {
    $okay_to_submit == 'no';
   } else {
    $okay_to_submit == 'yes';
   }
  }
}

 

This is the problem:

$_POST(mysql_result($result_field_desc_i, $cntr))

 

It just seems kind of ridiculous to me that putting a $_POST that is dependent on the loop in which it is contained doesn't work.

Link to comment
https://forums.phpfreaks.com/topic/113122-solved-why-isnt-this-possible/
Share on other sites

What are you trying to do? The $_POST superglobal is for returning data submitted from a form. You may aswell join the two queries in to one:

 $query_req_fields = "SELECT required FROM temp_tbl";
$result_req_fields = mysql_query($query_req_fields,$link);
$query_field_desc_i = "SELECT field_desc_i FROM temp_tbl";
$result_field_desc_i = mysql_query($query_field_desc_i,$link);

 

Aviod using mysql_result use one of the mysql_fetch_* functions instead, allows for much cleaner code.

 

Also make sure you use square brakets after the $_POST var, rather than parenthesis, eg:

$_POST['field_name']

not

$_POST('field_name')

I'm retrieving the field names to check and see if anything has been entered into them.

 

I have my reasons for using mysql_result.  Fetching the row/array/assoc doesn't work for what I've been trying to do for whatever reason.  I probably just don't know how to use them properly but they always either only return 1 result or none.

 

 

 

And I spoke too soon about the errors.  The error log says:

PHP Fatal error:  Function name must be a string in D:\\My Documents\\IRLCM_tech_form\\admin\\loop2.php on line 64, referer: http://xx.xx.xxx.xxx/admin/preview.php

 

 

How do I make it a string?

Okay this is getting really annoying...

 

I've seriously spent the last few hours on this one problem and my brain is fried now.  It makes absolutely no sense why this isn't doing what it's supposed to.  When I complete all of the fields in the test form, it still takes me to the page listing the required fields.

 

 $query_temp_tbl = "CREATE TEMPORARY TABLE temp_tbl SELECT * FROM reports WHERE report_name = ('$selected_report')";
mysql_query($query_temp_tbl);
$query_req_fields = "SELECT required FROM temp_tbl";
$result_req_fields = mysql_query($query_req_fields,$link);
$query_field_desc = "SELECT field_desc FROM temp_tbl";
$result_field_desc = mysql_query($query_field_desc,$link);
$query_field_desc_i = "SELECT field_desc_i FROM temp_tbl";
$result_field_desc_i = mysql_query($query_field_desc_i,$link);
  
for ($cntr = 0; $cntr < $num_rows; $cntr += 1) {
  if (mysql_result($result_req_fields, $cntr) == '1') {
   if ($_POST['mysql_result($query_field_desc_i, $cntr)'] == '') {
//also tried ($_POST[mysql_result($query_field_desc_i, $cntr)] == '')
    $okay_to_submit = 'no';
   } else {
    $okay_to_submit = 'yes';
   }
  }
}

echo "
<html>
<head>
  <title>Ingersoll Rand Preventative Maintenance Technician's Reports; Administration</title>
  <meta http-equiv=Content-Type content=text/html; charset=iso-8859-1>
";
if ($okay_to_submit == 'no') {
  echo "<meta http-equiv=refresh content=3;url='javascript:history.back();'>";
} else {
  echo "<meta http-equiv=refresh content=0.5;url='preview.php'>";
}
echo "
</head>
<body bgcolor=#CCCCCC>
  <table width=100% height=100% border=0 align=center valign=middle><tr><td align=center valign=middle>
   <table border=5 cellpadding=5><tr>
    <td align=center valign=middle bgcolor=#FFFFFF>
";
if ($okay_to_submit == 'no') {
  echo "
 <b>You must complete the following fields:</b><br>
  ";
  for ($cntr = 0; $cntr < $num_rows; $cntr += 1) {
   if (mysql_result($result_req_fields, $cntr) == '1') {
     echo mysql_result($result_field_desc, $cntr),"<br>";
   }
  }
} else {
  echo "Please wait while the database is updated...";
}
echo "
</td>
   </tr></table>
  </td></tr></table>
</body>
</html>
";

Sorry I wasn't clear earlier, change

if ($_POST['mysql_result($query_field_desc_i, $cntr)'] == '') {

to

if ($_POST[mysql_result($query_field_desc_i, $cntr)] == '') {

You don't need to use quotes in the $_POST array when a value is returned from a variable/function.

I've seriously spent the last few hours on this one problem and my brain is fried now.  It makes absolutely no sense why this isn't doing what it's supposed to.

 

It makes sense, you just have to get more comfortable with the language. This is a typical case of running before you can walk ;)

wildteen88, I tried that too.

 

 

It makes sense, you just have to get more comfortable with the language. This is a typical case of running before you can walk ;)

I'm comfortable enough with what I'm doing to know my logic is correct.  It's the syntax that always seems to get me, which is what I spend the majority of my time looking up.

 

 

This is getting rrrreally frustrating.  What am I doing wrong?

There MUST be something wrong with these statements:

 

 for ($cntr = 0; $cntr < $num_rows; $cntr += 1) {
  if (mysql_result($result_req_fields, $cntr) == '1') {
   if ($_POST[mysql_result($query_field_desc_i, $cntr)] == '') {

//I've also tried: if (mysql_result($result_req_fields, $cntr) == '1') AND ($_POST[mysql_result($query_field_desc_i, $cntr)] == '')
//Same thing really...


    $okay_to_submit = 'neg';
   } else {
    $okay_to_submit = 'pos';
   }
  }
}

 

";
if ($okay_to_submit == 'neg') {
  echo "<meta http-equiv=refresh content=3;url='javascript:history.back();'>";
}
if ($okay_to_submit == 'pos') {
  echo "<meta http-equiv=refresh content=0.5;url='preview.php'>";
}

 

if ($okay_to_submit == 'neg') {
  echo "
<b>You must complete the following fields:</b><br>
  ";
  for ($cntr = 0; $cntr < $num_rows; $cntr += 1) {
   if (mysql_result($result_req_fields, $cntr) == '1') {
     echo mysql_result($result_field_desc, $cntr),"<br>";
   }
  }
}
if ($okay_to_submit == 'pos') {
  echo "Please wait while the database is updated...";
}

 

 

Can anyone explain to me what it is?

Looking at your code, i disagree with you. The logic in your script is jumbled, and you're over complicating it with redundancies.

 

<?php

$q = "SELECT `required`, `field_desc_i` FROM `reports` WHERE `report_name` = '". mysql_real_escape_string($selected_report) ."'";
if ( ($r = mysql_query( $q, $link )) === FALSE )
exit( 'Query failed' );

$submit = TRUE;
while ( $data = mysql_fetch_assoc( $r ) ) {
if ( $data['required'] == '1' && empty($_POST[ $data['field_desc_i'] ]) )
	$submit = FALSE;
}

if ( $submit ) {

# Everything checks out

} else {

# A required field was left blank!

}

?>

You're good!  You've been doing this a while eh?  Thanks a lot.  I really do appreciate it.

 

 

And I actually was planning on going back and cleaning up the code.  I know a lot of it could have been simplified.

 

 

My problem is that I'm not aware of every command available.  I don't know all of the limitations or what all is already automated within php's core, so I try to work with what I already know works.  There's so many that it's hard to find exactly what you're looking for without some GOOD examples.  But I guess I'll never find out until I try huh?  It will take a loooong time though if all I do is trial and error.  And I don't think the tutorials/manuals on the net are very good.

 

On a side note, I do have my reasons for using mysql_result instead of mysql_fetch_assoc.  They're probably bad ones.  It's mainly because the query needs to be split up into sections (I have the page set up kind of weird/specifically...so it can't be done in one short, simple loop).  How would you change the mysql_fetch_assoc from a while loop to a for loop?  I never could get that to work, which is why I went with mysql_result.  I knew it worked for what I was trying to do.

My problem is that I'm not aware of every command available.  I don't know all of the limitations or what all is already automated within php's core, so I try to work with what I already know works.  There's so many that it's hard to find exactly what you're looking for without some GOOD examples.

 

For instance, I had no idea you could do this...

if ( $data['required'] == '1' && empty($_POST[ $data['field_desc_i'] ]) )

...or this...

$submit = TRUE;
//etc...
if ( $submit ) {

# Everything checks out

The PHP manual is actually an AMAZING resource! With great examples and priceless user notes. Rather than looking for something specific, be more broad!

 

Here's a list of mysql functions available to you, with short descriptions

http://php.net/manual/en/ref.mysql.php

 

You'll even see the mysql_fetch looping present in the Manual's examples

http://php.net/manual/en/function.mysql-fetch-assoc.php

 

And the below example if getting back into very basic syntax... take a look at the Language Reference within the manual

http://php.net/manual/en/langref.php

 

There you can find all the operators ( && can be found under logical ) and how to use then, your control and looping scrutctures, and everything you could imagine.

 

Don't be afraid to ask either. Many members are happy to answer PMs, and I even help people live via MSN Messenger if I have the time. There's no such thing as a stupid question, just don't get defensive if someone tells you to slow down and get back to the basics ;)

Also, if you're ever not sure, think it through the eyes of the PHP engine.

 

Here's the description for mysql_fetch_assoc

Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead.

 

So, say we have three result rows from a query, calling

while ( $data = mysql_fetch_assoc($result) ) {
   # some redundant code
}

Will fill $data with the results of the current (first) row, then move the internal pointer to the next row... the loop executes, and reaches the end. It will then fill $data with the results of the current (now second) row, then move the internal pointer to the third row... executes the loop and reaches the end. Same happens for the third row. Now there is no fourth row, so the mysql_fetch_assoc() function will return false, and the loop will stop executing.

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.