Jump to content

Should be quick - can someone fix this code? Variable declaration for SQL query


Stearmandriver

Recommended Posts

Hi folks...

 

Well it's true - Dreamweaver does write crappy PHP code.  Inelegance is one thing, but this is just plain wrong!  I think it must have irked Adobe to have to support an open-source technology... strangely, DW's support for Coldfusion is much better.  ::)

 

Anyhow, I'm trying to get a site thrown together for a relative.  I AM interested in learning PHP (and I'd love any recommendations on a good newbie book for someone with very little prior programming experience), but first things first - I gotta get this site working.

 

So... a simple search application.  User enters a form value on one page (using GET method) and gets a simple results list of product names.  Here's the problem:  DW apparently defines the variable it uses in the SQL query wrong.  It writes this:

 

$colname_rs_SEARCH = "-1";
if (isset($_GET['LAST_NAME'])) {
  $colname_rs_SEARCH = $_GET['LAST_NAME'];
}

 

I've only found one place where Adobe addresses this.  After changing the default value to %, I followed their advice which is in this format:

The isset condition seems to be reporting that the form parameter is set when in fact no value is submitted by the form. To fix the problem, change the if statement as follows:

 

$varLastName_rsTest = "%";
if ($_REQUEST["LastName"] != "") {
$varFirstName_rsTest = $_REQUEST["LastName"];
}

 

Different variable and table names, but you get the idea.  Even after changing my code to a similar format, it still doesn't work though and returns every row... so it's obviously still using the wildcard default value.

 

I hate to just ask someone to fix this code.... but could someone fix this code??  I really do want to learn it on my own, but for now I have to finish this project for the mother-in-law.  One thing at a time.  :P

 

If you don't want to spoon-feed me I can appreciate that.  I'd be glad for a reference to research on my own... anything!

 

Thanks much...

 

 

 

Link to comment
Share on other sites

First things first, avoid dreamweaver at all costs. Use Notepad++(free) or phpDesigner 2008(not free).

The best place to learn PHP is here or here.

 

Since I don't use dreamweaver, i'll show you how I would do it.

 

<?php
error_reporting(E_ALL); // Turn error reporting on.

// Put database connection code here.
/* 
if($con = mysql_connect('localhost', 'username', 'password')) // Create connection to database
  mysql_select_db('db_name') // Select database.
else { 
  die('<strong>MySQL Error: </strong>' . mysql_error()); // If it fails, display an error.
}
*/

$str = (isset($_POST['search'])) ? htmlentities(htmlspecialchars(mysql_real_escape_string($_POST['search']))) : false; // Secure POST data.

if(isset($_POST['submit'])) { // Check to see if the form has been submitted. If TRUE hide the form.
  $sql = sprintf("SELECT * FROM `db_name` . `table_name` WHERE `field_name` LIKE '%s'", '%' . $str . '%'); // MySQL query string with wildcards.
  $sql = mysql_query($sql);

  if(mysql_num_rows($sql) > 0)) { // Check to see if anything was returned from the query.
    while($row = mysql_fetch_array($sql)) { // Create a loop to loop through the database.
      print $row['table_name']; // Print the table name's in a loop.
      print '<br />';
    }
  }
  else { // No results returned from query.
    print 'Search returned no results.';
    print '<p>Please try again.</p>';
  }
}
else { // If form has not been submitted (returns FALSE), show the form.
?>
<html>
  <head>
    <title>Search</title>
  </head>
  <body>
    <form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
      <p>
        <label for="search">Search:</label><input type="text" name="search" id="search" />
        <input type="submit" name="submit" value="Search" />
      </p>
    </form>
  </body>
</html>
<?php
}

// mysql_close($con);
?>

 

I prefer POST data rather than GET when it comes to forms.

Link to comment
Share on other sites

people often forget the reason you go with post rather than get.

simple really

$_get == data gets put into url/ only 100 characters allowed

$_post == no data in url/ no character limit

 

 

i still dont understand why get is even there. i mean post is so much better.

Link to comment
Share on other sites

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.