PDA

View Full Version : saving information with post, and importing with row


gavimobile
01-08-2009, 01:49 AM
hey folks!
if im doing an update querey i would do this


<a href="link.php?upd=<?php $row['id']; ?>" >Update</a>
assuming my query is like this.


$upd = mysql_real_escape_string($_GET['upd']); //escaped get variable for query
$sql = ("SELECT * FROM table WHERE id = '$upd' "); //get all info for selected row
$rowUpd = mysql_fetch_array($sql);
so each field should look like this


<input value="<?php echo $rowUpd['my_row']; ?>" />
<!--pretend there is type & name & or id -->
this will show me the information for the selected user within the fields which is fine, but what ive been doing untill now was this


<input value="<?php echo $_POST['field']; ?>" />
<!--pretend there is type & name & or id -->
in order to save what the user typed before the page was in example refreshed or submited but rejected by validation.

so my main question is, how can i do both of them together?

like this sort of...


<input name="field" value="<?php if (isset($_GET['upd'])){ echo $rowUpd['my_row']; }else{ echo $_POST['field']; } ?>" />
tia

gavi

student101
01-13-2009, 08:06 AM
You have echo already, you can't echo again?
<?php echo((isset($_GET['upd']))? $rowUpd['my_row']:$_POST['field'] ?>
OR
<?php echo empty($_GET['upd'])? $rowUpd['my_row']:$_POST['field'] ?>

Cheers

gavimobile
01-13-2009, 12:54 PM
You have echo already, you can't echo again?
<?php echo((isset($_GET['upd']))? $rowUpd['my_row']:$_POST['field'] ?>
OR
<?php echo empty($_GET['upd'])? $rowUpd['my_row']:$_POST['field'] ?>Cheers
student101..thanks for the reply...this code looks weird to me as for the ? mark at the end of the function and the : between the variables. I didnt test if this piece of code would actually work, but could you possibly put this in a if statement so i can understand it better and put it to use acording to my understandings?

thanks

student101
01-13-2009, 01:08 PM
It's the shorthand of the looooong if then else statement
Your code says;
<?php if (isset($_GET['upd'])){ echo $rowUpd['my_row']; }else{ echo $_POST['field']; } ?>
Shorthand version;
<?php echo((isset($_GET['upd']))? $rowUpd['my_row']:$_POST['field'] ?>
My explanation;
echo((isset($_GET['upd']))? = if (isset($_GET['upd'])){
$rowUpd['my_row']: = echo $rowUpd['my_row'];
$_POST['field'] = }else{ echo $_POST['field']; }
This creates the shorthand version.

Hope it makes sense.

Cheers

Edit;

<?php
if (isset($_GET['upd'])){ // shouldn't this be if (isset($_POST['field'])){

echo $rowUpd['my_row'];
}else{
echo $_POST['field'];
}
?>

gavimobile
01-14-2009, 04:23 PM
It's the shorthand of the looooong if then else statement
Your code says;
<?php if (isset($_GET['upd'])){ echo $rowUpd['my_row']; }else{ echo $_POST['field']; } ?>Shorthand version;
<?php echo((isset($_GET['upd']))? $rowUpd['my_row']:$_POST['field'] ?>
My explanation;
echo((isset($_GET['upd']))? = if (isset($_GET['upd'])){
$rowUpd['my_row']: = echo $rowUpd['my_row'];
$_POST['field'] = }else{ echo $_POST['field']; }This creates the shorthand version.

Hope it makes sense.

Cheers

Edit;

<?php
if (isset($_GET['upd'])){ // shouldn't this be if (isset($_POST['field'])){

echo $rowUpd['my_row'];
}else{
echo $_POST['field'];
}
?>


hrm.. this is what i originally had/have

<?php if (isset($_GET['upd'])){ echo $rowUpd['my_row']; }else{ echo $_POST['field']; } ?>


this is what ur non shorthand version is

if (isset($_GET['upd'])){
echo $rowUpd['my_row'];
}else{ echo $_POST['field']; }


looks the same to me am i missing something?

student101
01-14-2009, 05:12 PM
I don't think I understand what it is you are trying to accomplish;
if (isset($_GET['upd'])){ // If the url = example.com?upd=something
echo $rowUpd['my_row']; // show me the field from the db
}else{ echo $_POST['field']; } //otherwise post back the data I just entered
Is that what you are trying to do?

Edit;
I have fully tested this idea of yours and it wont work.
You have to query the db to get the value $rowUpd['my_row'];

Try this;


<?php
$con = mysql_connect("localhost","user","pass");
if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("mydatabase", $con);
if (!empty($_GET["upd"])) {

$upd = mysql_real_escape_string($_GET["upd"]); //escaped get variable for query
$sql = ("SELECT * FROM table WHERE id = '$upd' "); //get all info for selected row
$row = mysql_query($sql) or die(mysql_error());
$rowUpd = mysql_fetch_assoc($row);

}
?>
<p><a href="?upd=197">send id</a></p>
<form name="frmxvalue" id="frmxvalue">
<input name="upd" value="<?php if ($_GET['upd'] != NULL){ echo $rowUpd['my_row']; }else{ echo $_GET['upd']; } ?>" />
<label>
<input type="submit" value="DO what?" />
</label>
</form>

student101
01-14-2009, 05:49 PM
It seems that the only way to do this is with two fields or with a complicated if then else

<p><a href="?upd=197">send id</a></p>
<form name="frmxvalue" id="frmxvalue">
Here I use if empy
<input name="upd" value="<?php echo empty($_GET['upd'])?$rowUpd['my_row']:$_GET['upd'];?>" />
<label>
<input type="submit" value="DO what?" />
</label>
</form>
<p>&nbsp;</p>
<form name="frmxvalue" id="frmxvalue">
Here I use if not empy
<input name="upd" value="<?php echo !empty($_GET['upd'])?$rowUpd['my_row']:$_GET['upd'];?>" />

<label>
<input type="submit" value="DO what?" />
</label>
</form>

gavimobile
01-14-2009, 05:55 PM
It seems that the only way to do this is with two fields or with a complicated if then else

<p><a href="?upd=197">send id</a></p>
<form name="frmxvalue" id="frmxvalue">
Here I use if empy
<input name="upd" value="<?php echo empty($_GET['upd'])?$rowUpd['my_row']:$_GET['upd'];?>" />
<label>
<input type="submit" value="DO what?" />
</label>
</form>
<p>&nbsp;</p>
<form name="frmxvalue" id="frmxvalue">
Here I use if not empy
<input name="upd" value="<?php echo !empty($_GET['upd'])?$rowUpd['my_row']:$_GET['upd'];?>" />

<label>
<input type="submit" value="DO what?" />
</label>
</form>

student101 thanks for the response.. that exactly what im trying to accomplish.. i didnt try your code yet, but i was querying in my original piece of code.. the code here in the form was just a sample to show my idea, my appoligies for not being clear.. anyways i will apply the code hopefully sometime this weekend and thank you very much for the response!!

gavimobile

student101
01-14-2009, 09:07 PM
I must be honest my logic is so retarded it's actually backwards. :confused:
My if then else statements only work after a couple of hours of playing.
It tooke me two hours to get this to work; :mrgreen:

if ($row_side['side'] != "left"){
$sides = "left";
}else{
$sides = "right";
}


Cheers