Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › PHP/SQL doesnt display properly?
New Posts  All Forums:Forum Nav:

PHP/SQL doesnt display properly?

post #1 of 5
Thread Starter 
If I run this:
Code:
<html>
<body>
<?php 
        
        $query = "select * from address where ".$searchtype."like '%".$searchterm."%'";
        
        $StreetAddress=$_POST['Street Address'];
        $Address2=$_POST['Address2'];
        $City=$_POST['City'];
        $State=$_POST['State'];
        $Zip=$_POST['Zipcode'];
        $Notes=$_POST['Site Notes'];
        
        //Required Address Fields
        if (!$StreetAddress || !$City || !$State) 
                {
                        echo "Please fill out the Street address, City, and State and resubmit";
                        exit;
                }
                
        if (!get_magic_quotes_gpc())
                {
                        $StreetAddress = addslashes($StreetAddress);
                        $Address2 = addslashes($Address2);
                        $City = addslashes($City);
                        $State = addslashes($State);
                        $Zip = addslashes($Zip);
                        $Notes = addslashes($Notes);
                
        //Connect to Database
        @$db = new mysqli('xxxxxxxx', 'xxxxxxx', 'xxxxxx', 'XXXXXXX');
        
        //Display Error upon failed connection
        if (mysqli_connect_errno())
                {
                        echo "Error: Could not connect to database. Please try again later.";
                        exit;
                        
                }
                
        $query = "insert into address values
                                ('".$StreetAddress."', '".$Address2."', '".$City."', '".$State."', '".$Zip."', '".$Notes."')";
                                
                                $result=$db->query($query);
                                
        if ($result)
                {
                        echo $db->affected_rows." Address entered into database.";
                }else
                {
                                echo "An error has occured. The address was not added.";
                }
                        
        $db->close();
        
        ?>
    
        </body>
    </html>


I get this:
Code:
query($query); if ($result) { echo $db->affected_rows." Address entered into database."; }else { echo "An error has occurred. The address was not added."; } $db->close(); ?>

I have Bolded the problem line. The problem line is just above the last if-else. I have re-written it several times/ways to make sure that wasnt the case either. To double check i wrote it exactly as it is from a tutorial/book I have. Fair warning this is my first PHP/SQL integration so any generic tips would also be appreciates.

Thank you!!
Edited by DanNEBTD - 5/4/12 at 7:34pm
    
CPUMotherboardGraphicsRAM
Opty 170 ccb1e 0609fpaw A8N32-SLI EVGA 7950X2 2 gigs Gskill PC4000 
Hard DriveOptical DriveOSMonitor
4x 36gig Raptors raid 0, 1x 200gig storage Lightscribe DVD R/w Vista Home Premium x86 32" Syntax Olevia 
KeyboardPowerCaseMouse
G15 PC&P Silencer 750 Quad Mozart Tx Revolution/G5 
Mouse Pad
Banshee 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Opty 170 ccb1e 0609fpaw A8N32-SLI EVGA 7950X2 2 gigs Gskill PC4000 
Hard DriveOptical DriveOSMonitor
4x 36gig Raptors raid 0, 1x 200gig storage Lightscribe DVD R/w Vista Home Premium x86 32" Syntax Olevia 
KeyboardPowerCaseMouse
G15 PC&P Silencer 750 Quad Mozart Tx Revolution/G5 
Mouse Pad
Banshee 
  hide details  
Reply
post #2 of 5
I dont know if im missing somthing on there, but where are you getting the 2 variables in the query from?
  1. $searchtype
  2. $searchterm

If you have them from a form you have to include the form method etc.
Code:
$_GET['searchtype']

That's just the first thing that struck me so if that does not fix the problem or isn't the case i'll look a little deeper smile.gif
Shiiva
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom x6 1090t @ 4Ghz Asus Crosshair IV Powercolor HD6970 8 gb Corsair Vengance 
Hard DriveOptical DriveCoolingOS
Corsair SSD Force Series 3, 120GB Samsung RW sata Corsair Hydro H50 Windows 7 professional 
MonitorKeyboardPowerCase
Yamakasi Catleap Logitech G15 Corsair 750w CM HAF 932 Big 
MouseMouse PadAudio
Logitech G5 Razer destructor Razer Carchariaz 
  hide details  
Reply
Shiiva
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom x6 1090t @ 4Ghz Asus Crosshair IV Powercolor HD6970 8 gb Corsair Vengance 
Hard DriveOptical DriveCoolingOS
Corsair SSD Force Series 3, 120GB Samsung RW sata Corsair Hydro H50 Windows 7 professional 
MonitorKeyboardPowerCase
Yamakasi Catleap Logitech G15 Corsair 750w CM HAF 932 Big 
MouseMouse PadAudio
Logitech G5 Razer destructor Razer Carchariaz 
  hide details  
Reply
post #3 of 5
Quote:
Originally Posted by Nishinku View Post

I dont know if im missing somthing on there, but where are you getting the 2 variables in the query from?
  1. $searchtype
  2. $searchterm
If you have them from a form you have to include the form method etc.
Code:
$_GET['searchtype']
That's just the first thing that struck me so if that does not fix the problem or isn't the case i'll look a little deeper smile.gif

If you look higher up in the code you see he grabs them from a POSTed form.

Question: Are ALL the columns of the table you're trying to update being updated? That is the only way you can use the short-hand SQL insert statement, otherwise you'll need to explicitly declare everything:
INSERT INTO table_name (Column1, Column2, Column3) VALUES ('Value1', 'Value2', 'Value3');


Once we fix this problem we should also have a quick chat about SQL injection tongue.gif
post #4 of 5
Quote:
Originally Posted by crazyap7 View Post

If you look higher up in the code you see he grabs them from a POSTed form.
Question: Are ALL the columns of the table you're trying to update being updated? That is the only way you can use the short-hand SQL insert statement, otherwise you'll need to explicitly declare everything:
INSERT INTO table_name (Column1, Column2, Column3) VALUES ('Value1', 'Value2', 'Value3');
Once we fix this problem we should also have a quick chat about SQL injection tongue.gif

But in line 5 hes using normal variables in the sql sentence without anywhere putting the post information into them.
Code:
$query = "select * from address where ".$searchtype."like '%".$searchterm."%'";

He should then have written.
Code:
$searchtype = $_POST['searchtype'];
$searchterm = $_POST['searchterm'];
$query = "select * from address where ".$searchtype."like '%".$searchterm."%'";

Or am i missing some code shortcut that i was unaware of?
Shiiva
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom x6 1090t @ 4Ghz Asus Crosshair IV Powercolor HD6970 8 gb Corsair Vengance 
Hard DriveOptical DriveCoolingOS
Corsair SSD Force Series 3, 120GB Samsung RW sata Corsair Hydro H50 Windows 7 professional 
MonitorKeyboardPowerCase
Yamakasi Catleap Logitech G15 Corsair 750w CM HAF 932 Big 
MouseMouse PadAudio
Logitech G5 Razer destructor Razer Carchariaz 
  hide details  
Reply
Shiiva
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom x6 1090t @ 4Ghz Asus Crosshair IV Powercolor HD6970 8 gb Corsair Vengance 
Hard DriveOptical DriveCoolingOS
Corsair SSD Force Series 3, 120GB Samsung RW sata Corsair Hydro H50 Windows 7 professional 
MonitorKeyboardPowerCase
Yamakasi Catleap Logitech G15 Corsair 750w CM HAF 932 Big 
MouseMouse PadAudio
Logitech G5 Razer destructor Razer Carchariaz 
  hide details  
Reply
post #5 of 5
Oh yeah man sorry, I assumed he'd defined those earlier, in a part of the code he didn't put up, since the error message he gets isn't related to it.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › PHP/SQL doesnt display properly?