What is going wrong with this mysql insert query?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

What is going wrong with this mysql insert query?

Post by aditya2071990 »

Hi people,

I almost finished a part of a script I am trying to make, and there seems to be a very little Gimp that is just ruining it all... please help me figure out what is going wrong?

And the problem is that, though there is no error from mysql or php parser, the data that is in the variables is not getting inserted into the db, no matter what.

I am including all the code that I wrote...

If you want to get to it quick, the mysql query is on the 145th line:

Code: Select all

session_start();
 
$ad_package = $_SESSION['ad_package'];
 
function mysql_conn(){
    
    $connect = @mysql_connect('localhost','root');
    if(!$connect){
        
        echo 'Unable to connect to database server';
        exit();
        
    };
    $use = @mysql_query('use wish4ababy;');
    if(!$use){
    
        echo 'Unable to connect to required database.';
        exit();
        
    };
};
 
mysql_conn();
 
global $first_name;
 
$first_name = mysql_real_escape_string($_POST['first_name']);
 
$last_name = mysql_real_escape_string($_POST['last_name']);
 
$address1 = mysql_real_escape_string($_POST['address1']);
 
$address2 = mysql_real_escape_string($_POST['address2']);
 
$city = mysql_real_escape_string($_POST['city']);
 
$zip = mysql_real_escape_string($_POST['zip']);
 
$email = mysql_real_escape_string($_POST['email']);
 
$contact_num = mysql_real_escape_string($_POST['contact_num']);
 
$heading = mysql_real_escape_string($_POST['heading']);
 
$adcontent = mysql_real_escape_string($_POST['content']);
 
$adlocation_generic = $_SESSION['ad_location'];
 
if($adlocation_generic == 'a_state'){
 
    $adlocation_detailed = $_SESSION['which_state'];
    
}else if($adlocation_generic == 'a_city'){
 
    $adlocation_detailed = $_SESSION['city_name'];
    
};
if(isset($adlocation_detailed)){
 
    $adlocation = $adlocation_detailed;
    
}else{
 
    $adlocation = $adlocation_generic;
    
};
 
$adtype = $ad_package;
 
function getUserIP(){
    
    $userIPActual = $_SERVER['REMOTE_ADDR'];
    global $userIP;
    $userIP = str_replace('.','',$userIPActual);
    return  $userIP;
    
};
 
function getTime(){
    
    $microTimeActual = microtime(true);
    global $microTime;
    $microTime = str_replace('.','',$microTimeActual);
    return  $microTime;
    
};
 
function generateaRandom(){
    
    function generateRandom($input){
            
            $random_number = intval( "0" . rand(1,9) . rand(0,9) . rand(0,9) . rand(0,9) . rand(0,9) ); // random(ish) 5 digit int
        
            $random_string = chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)); // random(ish) 5 character string
            
            $randomSum = $random_number.$random_string.'adjafhdklf';
            
            $mostRandom = md5($randomSum.$input);
            
            return $mostRandom;
        
        };
    
    function generate_a_random(){
    
        $a_random_string = generateRandom(generateRandom(generateRandom(generateRandom(2071990))));
        
        return $a_random_string;
    
    };
    
    global $final_random;
    $final_random = generate_a_random();
    
    return $final_random;
    
};
 
getUserIP(); getTime(); generateaRandom();
 
$unique_ad_id = $userIP.$microTime.$final_random;
 
//image control
 
 function  imgControl(){
    
    global $img_assignable;
    $img_source = $_POST['image'];
    $img_cur_name = $_FILES['image']['tmp_name'];
    $img_type = $_FILES['image']['type'];
    $img_type = str_replace('/','',$img_type);
    $img_type = str_replace('image','',$img_type);
    $img_type = str_replace('pjpeg','jpeg',$img_type);
    $img_type = str_replace('x-png','png',$img_type);
    $img_new_name = $unique_ad_id.'.'.$img_type;
    $img_assignable = 'display_ad_images/'.$img_new_name.'';
    copy(''.$img_cur_name.'',''.$img_assignable.'');
        
};
 
//end of image control
 
function insert_data(){
 
    $insert = @mysql_query("INSERT INTO ad_details (id ,first_name ,last_name ,address1 ,address2 ,city ,zip ,email_id ,contact_num ,ad_heading ,ad_content ,image_name ,ad_location ,ad_type ,ad_date ,unique_ad_id ) VALUES ('', '$first_name', '$second_name', '$address1', '$address2', '$city', '$zip', '$email', '$contact_num', '$heading', '$adcontent', '$img_assignable', '$adlocation', '$adtype', CURDATE(), '$unique_ad_id');");
 
    if(!$insert){
        
        echo 'The ad insertion failed!';
        echo mysql_error();
        
    };
 
};
 
$submit = $_POST['submit'];
 
if(isset($submit)){
    
    insert_data();
    exit();
    
};
And please guys, though I would really love to see your comments on how the code can be made better, it is doing its job, though with a rudimentary approach. I will definitely come back to optimizing it, but for now, setting it up is my primary concern...

Look forward to all your replies...
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: What is going wrong with this mysql insert query?

Post by aceconcepts »

Remove this semi-colon fromt he query on line 145:

Code: Select all

'$unique_ad_id');"
Also, I tend to set variables as date functions outside of a query.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: What is going wrong with this mysql insert query?

Post by aditya2071990 »

Thank you, aceconcepts, I will also do what you suggested, but I did something:

Instead of wrapping the query in the insert_data() function, I simply put it up inside the conditional if(isset($submit)) conditional, and it worked! Wonder why that happened, though. :?

Now that it works of course, you can go on and criticize all you want... :D
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: What is going wrong with this mysql insert query?

Post by aceconcepts »

Hey dude, if it works, it works :D
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: What is going wrong with this mysql insert query?

Post by aditya2071990 »

Haha that's right :)
Post Reply