PHP forums, MySQL forums, Web Development resources

Home PageHome    PHP ResourcesTopic List    FAQFAQ    SearchSearch    MemberlistMemberlist    UsergroupsUsergroups 
 RegisterRegister
    ProfileProfile    Log in to check your private messagesLog in to check your private messages    Download the RSS Reader RSS Feed Download the RSS Reader RSS for this forum Log inLog in 

PHP Forum :: MySQL Forum :: Java Script Forum



PHP interface to query MySQL problem

 
Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General
View previous topic :: View next topic  
Author Message
stilllearning



Joined: 02 Mar 2010
Posts: 4

PostPosted: Tue Mar 02, 2010 1:21 am    Post subject: PHP interface to query MySQL problem Reply with quote

Hi

I am trying to create a search in mysql using php for the UI. Basically my website has 11 fields. Any of these fields can be filled out, but then I need to pass the completed fields on to query mysql while the blank fields are excluded. For e.g. I have customername, customerlastname, StreetAddress, PhoneNumber, CustID, DOB etc etc. When I fill out customername and StreetAddress, I need the script to query mysql with only these values.

Obviously this is just 1 example. Sometimes more fields can be filled out and sometimes fewer fields can be filled out. The point is the mysql query must only use the filled out fields and not the empty fields.

I am still too new with PHP to know how to do this and will appreciate any help.

TIA
willemvw
Back to top
View user's profile Send private message
lostboy



Joined: 02 May 2004
Posts: 6059
Location: toronto, canada

PostPosted: Tue Mar 02, 2010 4:12 pm    Post subject: Reply with quote

There is an [url=http://www.weberdev.com/get_example-4236.html]example with code here[/url]
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
stilllearning



Joined: 02 Mar 2010
Posts: 4

PostPosted: Wed Mar 17, 2010 12:21 am    Post subject: Reply with quote

Hi Lostboy

Thank you for your help with this. I have managed to get the script working beautifully.

I am now onto my next (and luckily last) problem with php. I am trying to use the query script you posted to turn it into a delete script. HOwever I keep getting

"Can't connect because You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE CustomerName LIKE 'Test 2' ''' at line 1" I am a little lost and would appreciate some help please.

The line I am changing is if ((!empty($_POST['CustomerName']))&&($_POST['CustomerName'] != 'all'))
{
$sql .= " WHERE CustomerName LIKE '". addslashes($_POST['CustomerName'])."' '$CustomerName' ";
}

From your script you had "cuenta" where I have CustomerName. From the "idclase" I have taken all that out of my delete script as I only need to select the Customer Name to delete a record from the DB.

TIA
Back to top
View user's profile Send private message
stilllearning



Joined: 02 Mar 2010
Posts: 4

PostPosted: Wed Mar 17, 2010 12:25 am    Post subject: Reply with quote

Just a quick update:

I changed the WHERE to and in the code I posted below and am now getting no "records found!" This is a step forward although it's still not working the way it should.
Back to top
View user's profile Send private message
lostboy



Joined: 02 May 2004
Posts: 6059
Location: toronto, canada

PostPosted: Wed Mar 17, 2010 5:03 pm    Post subject: Reply with quote

Never use customerName as a key, what if you have 2 John Smiths? Each record should have a unique primary key (autonumber, hash, whatever) but it needs to be unique so that you don't kill incorrect records.

That said, a simple way to test your sql is to echo it out onto the page and then copy it to phpmyadmin or another gui db tool. I think in this case, you will have a funky sql because of the post and the customerName variable

[code:1]

if ((!empty($_POST['CustomerName']))&&($_POST['CustomerName'] != 'all'))
{
$sql .= " WHERE CustomerName LIKE '". addslashes($_POST['CustomerName'])."' ";
}

[/code:1] would be more like what you need
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
stilllearning



Joined: 02 Mar 2010
Posts: 4

PostPosted: Thu Apr 08, 2010 7:15 am    Post subject: Reply with quote

Hi Lostboy

I have been trying the code change you posted to delete a field in MySQL, but so far have had no success. Basically my current code looks like this:

function search()
{
$sql = "SELECT * FROM Circuits "; // WHERE CustomerName LIKE '$CustomerName'";
if ((!empty($_POST['CustomerName']))&&($_POST['CustomerName'] != 'all'))
{
$sql .= " WHERE CustomerName LIKE '". addslashes($_POST['$CustomerName'])."' ";
}
$result = conn($sql);

if (!$result){ die("No results due to database error.<br>".mysql_error()); }

if (mysql_num_rows($result)==0)
{
echo "No Results found!";
}else{

echo "Deleting the entry ...</br>";

mysql_query("DELETE FROM Circuits WHERE CustomerName = '$sql'");

echo "Entry DELETED!!!!";

}

The above gives me no records found, but the php code shows me in the dropdown list the name I want to delete is there. Same when I do a manual search in MySQL, the name is there.

If I change the line
$sql = "SELECT * FROM Circuits "; // WHERE CustomerName LIKE '$CustomerName'";
to
$sql = "SELECT * FROM Circuits WHERE CustomerName LIKE '$CustomerName'";
ie take out the // I get an error in MySQL code which makes sense as there is a line further down ($sql .= " WHERE CustomerName LIKE '")
which duplicates the first line.

I then went on to change the $sql statements in this function to $sql1 as I thought maybe the $sql variables could be public and thus interfere with each other. (This is just a noob thinking aloud, so please don't laugh ok?) I then get the error "Can't connect cause Query is empty." I have been scratching my head and re-reading my php code numerous times, but for the life of me I cannot seem to get this solved.

Please can you help me?

TIA
Back to top
View user's profile Send private message
vipin kumar



Joined: 05 Apr 2010
Posts: 26

PostPosted: Thu Apr 08, 2010 11:48 am    Post subject: Reply with quote

I am also facing this problem.
Back to top
View user's profile Send private message Visit poster's website
lostboy



Joined: 02 May 2004
Posts: 6059
Location: toronto, canada

PostPosted: Fri Apr 16, 2010 4:48 pm    Post subject: Reply with quote

break stuff into smaller functions

[code:1]
if(isset($POST['submit'])){
$id = search();
if($id){ delete($id); }

}

function delete($id){
echo "Deleting the entry ...<br />";

mysql_query("DELETE FROM Circuits WHERE CustomerName = '$sql'");

echo "Entry DELETED!!!!";


}//end function

function search()
{
$sql = "SELECT * FROM Circuits "; // WHERE CustomerName LIKE '$CustomerName'";
if ((!empty($_POST['CustomerName']))&&($_POST['CustomerName'] != 'all'))
{
$sql .= " WHERE CustomerName LIKE '". addslashes($_POST['$CustomerName'])."' ";
}
$result = conn($sql);

if (!$result){ die("No results due to database error.<br />".mysql_error()); }

if (mysql_num_rows($result)==0)
{
echo "No Results found!";
}else{

//question: is there a possibility of having more than one record here?
//assume no
$record_id = mysql_result($result, 0, 'row_id'); //hopefully there is some unique row id

}
return $record_id;
}//end function
[/code:1]
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
Display posts from previous:   
WeberTrivia Questions WeberTrivia Questions
 Think you are smart? Prove it!. Try your skills with these questions :
 WeberTrivia QuestionsRecursive arrays and multi-dimensional arrays are one and the same. (PHP and MySQL)
 WeberTrivia QuestionsThe \"cache_dir\" tag of the squid configuration has a default of /var/spool/squid. (Linux)

WeberTrivia Questions



PHP Code Examples
 Stream diffrent sizes of images from a single image to save disk space.
 JavaScript dropdown list menu to switch any page.
 Dump the contents of a PHP variable in html format with a recursive list of subfolders and files from a given root directory.
 PHP Dump in html format the contents of one array variable with a recursive list of the nested array variables inside.
 Link Extractor - This function is used to extract links from a given URL. This will convert relative path into absolute path and also remove PHPSESSID stuff.
 Building a Dynamic Form using Javascript and innerHTML. Add form elements in realtime without refreshing the page.
 A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Powered by phpBB © 2001, 2005 phpBB Group
PHP Forum :: MySQL Forum :: Java Script Forum