PDA

View Full Version : $_GET question


Jacobb123
09-29-2007, 02:11 AM
I am trying to run a query and I can't get the following to work


$userid=$_GET['userid'];
mysql_query("insert into photos values('','$resimadi','$uploaddirurl', '$userid')") or die(mysql_error());

it keeps giving a value of 0 in the DB

Inigoesdr
09-29-2007, 02:47 AM
$userid = intval($_GET['userid']);
$query = "INSERT INTO `photos` VALUES(NULL,'$resimadi','$uploaddirurl', '$userid')";
echo $query;
mysql_query($query) or die(mysql_error());

Run that. You should generally specify the columns in your query too, to avoid problems when you change the columns in the database.

Jacobb123
09-29-2007, 03:13 AM
That didn't work.....it keeps echoing a 0 for the user id.

Here is what I have

$userid = intval($_GET['userid']);
$query = "INSERT INTO `photos` VALUES(NULL,'$resimadi','$uploaddirurl', '$userid')";
echo $query;


edit_profile.php?userid=2

Fumigator
09-29-2007, 03:33 AM
What does your "echo $query;" statement show you?

Is your query giving you an error? (You are checking to see if the query produces an error, right?)

What data type is your userid column?

What does your table layout look like?

Jacobb123
09-29-2007, 03:44 AM
What does your "echo $query;" statement show you?

Is your query giving you an error? (You are checking to see if the query produces an error, right?)

What data type is your userid column?

What does your table layout look like?

INSERT INTO `photos` VALUES(NULL,'20070928104136_Photo 2.jpg','http://bizreport.tv/photos/', '0')

There is no error


`photos` (
`id` int(50) NOT NULL auto_increment,
`file` text NOT NULL,
`location` varchar(50) NOT NULL default '',
`company_id` int(50) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=17 ;

Fumigator
09-29-2007, 03:59 AM
I hate to point out the obvious, but do you see that '0' in your query? Yeah... also your column 'id' is the second column in the table, yet you are inserting the 'id' value into the fifth column. That's why you should specify the columns you are inserting into, as Inigoesdr already mentioned, so the order doesn't matter.

Jacobb123
09-29-2007, 04:19 AM
I don't understand what you are saying.

The first column is id which is auto_incremented. The fifth is pulling the information from edit_profile.php?userid=2

SO they are not the same. I am really confused at your statement. The fifth column should be inserting "2" into the company_id" field

Inigoesdr
09-29-2007, 04:34 AM
The fifth is pulling the information from edit_profile.php?userid=2

Obviously it is not pulling the information from the $_GET variable. It's getting overwritten or something before that.

CFMaBiSmAd
09-29-2007, 04:50 AM
I'm in agreement with Inigoesdr.

Some possibilities -

The actual link does not contain a ?userid=2 parameter at all, which produces a 0 in the query (tested on my development system).

The value on the link is actually ?userid=0

The $_GET is not functioning on your server (unlikely.)

The $_GET contains some leading character(s) which prevents the intval() from finding a numeric value, which results in a 0 (see the intval() definition in the php manual.) Echo the $_GET variable to see if it at least echos a 2. A trim() might be necessary to get it into a form that the intval() can produce a number from.

Something in your code is inadvertently clearing the $_GET variable (post your actual code up to the $query = "...."; statement.)

Check your web server log for errors and/or turn on full php error reporting to see if notices/warnings are occurring that would point to why it is not working.

Short answer - the posted url and the few lines of code shown to us do produce a query string with the '2' in the last value field (tested on my development system.) Therefore, you have something going on that is preventing the value from reaching that position.

Fumigator
09-29-2007, 04:50 AM
SO they are not the same. I am really confused at your statement. The fifth column should be inserting "2" into the company_id" field

Whoops my bad, I glanced over the layout too quickly. You are alright on that note, it's just that you have a 0 in the $_GET['userid'] variable.

CFMaBiSmAd
09-29-2007, 04:59 AM
I just thought of a possibility. If register_globals are on and there is some other global variable registered with the name $userid, such as a session/cookie/regular variable, then the $_GET variable with that same name will be overwritten.

If register_globals are on -

A) Change the name of your url parameter and the $_GET['...'] variable to something else.

B) As quickly as you can, turn register_globals OFF.

Jacobb123
09-29-2007, 06:03 AM
OK.....Here are the two files that I am working with. I am including the upload script in another file edit_profile.php. Someone help me out because I can't figure this out

Edit_profile.php

<?php
include 'site_header.php'; ?>

<?php
ini_set('session.use_only_cookie', 1);
session_start();
include 'dbconnect.php';
opendb();
?>

<body>



<?php

$photo_query=mysql_query("SELECT * FROM photos where company_id ='{$_GET['company_id']}'");
$user_info=mysql_query("SELECT * FROM tbluser where userid = '{$_GET['userid']}'");
$row=mysql_fetch_array($user_info) or die(mysql_error());



?>
<div id="floatleft">
<form action = "update_user.php" method="POST"><table>

<tr><td width="25%">Username</td><td><input type="text" name="username" value = "<?php echo stripslashes($row['username']); ?>"></td></tr>

<tr><td width="25%"> Password</td><td><input type="text" name="userpass" value = "<?php echo stripslashes($row['userpass']); ?>"></td></tr>

<tr><td width="25%"> Email</td><td><input type="text" name="useremail" value = "<?php echo stripslashes($row['useremail']); ?>">
<?php echo '<input type = "hidden" name="userlvl" value="'.$_SESSION['userlvl'].'">'; ?>
</td></tr>
<?php
if($_SESSION['userlvl']==99){
?>
<tr><td width="25%"> User level</td><td>
<select name ="userlvl">
<?php
switch($row['userlvl']){
case 0:
?>
<option selected value="0">Client</option>
<option value="1">User</option>
<option value="99">Admin</option>
<?php
break;
case 1:
?>
<option value="0">Client</option>
<option selected value="1">User</option>
<option value="99">Admin</option>
<?php
break;
case 99:
?>
<option value="0">Client</option>
<option value="1">User</option>
<option selected value="99">Admin</option>
<?php
break;
?>
</select>
</td></tr>
<?php
}
}
?>
<tr><td width="25%"> Name</td><td><input type="text" name="firstname" value = "<?php echo stripslashes($row['name']); ?>"></td></tr>

<tr><td width="25%"> Phone</td><td><input type="text" name="phone" value = "<?php echo stripslashes($row['phone']); ?>"></td></tr>

<tr><td width="25%"> Address</td><td><input type="text" name="address1" value = "<?php echo stripslashes($row['address1']); ?>"></td></tr>

<tr><td width="25%"> Address 2</td><td><input type="text" name="address2" value = "<?php echo stripslashes($row['address2']); ?>"></td></tr>

<tr><td width="25%"> City</td><td><input type="text" name="city" value = "<?php echo stripslashes($row['city']); ?>"></td></tr>

<tr><td width="25%"> State</td><td><input type="text" name="nstate" value = "<?php echo stripslashes($row['nstate']); ?>"></td></tr>

<tr><td width="25%"> Zip</td><td><input type="text" name="nzip" value = "<?php echo stripslashes($row['nzip']); ?>"></td></tr>

<tr>
<td width="25%"> Country</td>
<td><input type="text" name="country" value = "<?php echo stripslashes($row['country']); ?>"></td></tr>

<tr><td colspan="2"><div align="center"><input type = "hidden" name = "userid" value="<?php echo $_GET['userid']; ?>"><input type="submit" name="Submit" value="Submit"></div></td></tr></table></form></div>
<div id="floatright">
<?


include 'photo_upload.php';
//while($photo=mysql_fetch_array($photo_query)
//{

//echo '<div id="floatright"><img src='$photo['']'</div>

//}

?>

</div>
</div>

</body>
</html>


Photo_upload.php

<?
$connection = mysql_connect(") or die (mysql_error());
mysql_select_db("", $connection) or die (mysql_error());
$uploaddir="../photos/";
$uploaddirurl = "http://bizreport.tv/photos/";

?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1254">
<style type="text/css">
<!--
.yazi {font-family: Arial, Helvetica, sans-serif; font-size: 12px}
.kalin {font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; }
.tab1 {
font-family: Arial, Helvetica, sans-serif;
color: #FFFFFF;
}
.link {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-style: italic;
line-height: normal;
color: #616161;
text-decoration: none;
}
-->
</style>
</head>

<fieldset><legend><font class="kalin" color=blue><b>File Upload</b></font></legend>
<?
if($hz=="eklez"){
$resimadi = date("Ymdhis");
$resimadi = $resimadi . "_" . $_FILES['userfile']['name'];
$uploadfile = $uploaddir . $resimadi;
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);


$query = "INSERT INTO `photos` (`id`,`file`,`location`,`company_id`) VALUES(NULL,'$resimadi','$uploaddirurl', '$company_id')";
echo $query;

mysql_query($query) or die(mysql_error());
echo "<center><br><h1><a href=\"$uploaddir$resimadi\">$resimadi</a> File was uploaded</h1><br><br><a href=\"javascript:history.go(-1);\"><< Back </a></center>\n";
}else{
?>
<form action="photo_upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="hz" value="eklez">
<table border="0" width="100%">
<tr><td class=kalin>File</td><td><input type="file" name="userfile"></td></tr>
<tr><td colspan=2><input type="submit" value="Upload"></td></tr>
</table>
</form>
<? } ?>
</fieldset>

Fumigator
09-29-2007, 06:22 AM
Well that was a merry goose chase you led us on... your original problem isn't anything like your actual code! How do you expect a decent answer doing that? Sheesh.

Your INSERT query isn't executed until the form in photo_upload.php is submitted via POST, which means your $_GET array is empty by that time and the variable $_GET['company_id'] is long gone (and the variable $company_id along with it).

CFMaBiSmAd
09-29-2007, 06:55 AM
O'boy, I don't have anything kind to say about that code or the game of 20 questions/wild goose chase it took to get to this point.

Unless all the servers where you plan on running this code have output buffering turned on in php.ini or you use output buffering in your code (I don't see any), your sessions won't work because the following code is outputting a newline to the browser prior to the session_start(), between the excessive and un-necessary php closing and opening tags. I am also guessing that your site_header.php file is outputting html content, which if it is working now, is only working because output buffering is turned on in php.ini where you are developing this code, and won't necessarily work on a production server -
<?php
include 'site_header.php'; ?>
<---- this newline will prevent session_start() unless output buffering is on or explicitly used
<?php
ini_set('session.use_only_cookie', 1);
session_start();

Inigoesdr
09-29-2007, 05:59 PM
You're also missing a " in photo_upload.php