Reply
If blank then dont Update
Old 11-04-2009, 07:43 PM If blank then dont Update
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
How do I do it so when this page gets run by itself it doesn't update the table, since if it does it will just make all the fields blank.

PHP Code:
<?php
require_once ('inc/config.php');
$firstname mysql_real_escape_string ($_POST['firstname']);
$lastname mysql_real_escape_string ($_POST['lastname']);
$middlenamemysql_real_escape_string ($_POST['middlename']);
$id 2;  
$sql mysql_query ("
   UPDATE `testing` SET 
      `FirstName` = '"
.$firstname."', 
      `LastName` = '"
.$lastname."',
      `MiddleName` = '"
.$middlename."' 
   WHERE `id` = '"
.$id."'
"
)
OR die (
mysql_error());
?>
Instead make it update the table update only when a form updates it?
__________________
MY MSN: Sith717@Hotmail.com
PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 11-04-2009, 07:58 PM Re: If blank then dont Update
orionoreo's Avatar
Ultra Talker

Posts: 261
Name: Jerry
Trades: 0
side question... how come your sig says php and web design professionally done... pm for html, php help when you're asking a lot of basic information

don't take it offensively I'm just curious

PHP Code:
<?php

<?php
require_once ('inc/config.php');
$firstname mysql_real_escape_string ($_POST['firstname']);
$lastname mysql_real_escape_string ($_POST['lastname']);
$middlenamemysql_real_escape_string ($_POST['middlename']);
$id 2;  

if (
$firstname=='' || $lastname=='' || $middlename=='') {
    
$sql mysql_query ("
       UPDATE `testing` SET 
          `FirstName` = '"
.$firstname."', 
          `LastName` = '"
.$lastname."',
          `MiddleName` = '"
.$middlename."' 
       WHERE `id` = '"
.$id."'
    "
)
    OR die (
mysql_error());
}
?>
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 08:00 PM Re: If blank then dont Update
chrishirst's Avatar
Super Moderator

Posts: 22,237
Location: Blackpool. UK
Trades: 0
PHP Code:
if($_POST['submit_button_name'] != "" ) {
    do 
update
} else {
    do 
not update;

__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-04-2009, 08:04 PM Re: If blank then dont Update
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
Thanks I used something a little more simple now I have another question...

I am trying to do it so the value in the text box is the current value that is in the database so I do not have to retype, and instead edit it or add on to it.

PHP Code:
<html>
<body>
<?php
if (isset ($_POST['submit'])) // if the form was submitted, display their name
{
require_once (
'inc/config.php');
$firstname mysql_real_escape_string ($_POST['firstname']);
$lastname mysql_real_escape_string ($_POST['lastname']);
$middlenamemysql_real_escape_string ($_POST['middlename']);
$id 2;  
$sql mysql_query ("
   UPDATE `testing` SET 
      `FirstName` = '"
.$firstname."', 
      `LastName` = '"
.$lastname."',
      `MiddleName` = '"
.$middlename."' 
   WHERE `id` = '"
.$id."'
"
)
OR die (
mysql_error());
echo 
"hello";
}
//form hasent been submitted
{

?>


<form action="<?php echo $_SERVER['PHP_SELF'?>" method="post">
Firstname: <input type="text" value="<?php echo $row['FirstName'];?>" name="firstname" /><br>
Lastname: <input type="text" value="<?php echo $row['LastName'];?>" name="lastname" /><br>
middlename: <input type="text" value="<?php echo $row['MiddleName'];?>" name="middlename" /><br>
<input type="submit" id="submit" name="submit" value="Submit!">
</form>
<?php
}
?>


</body>
</html>
Basicly the value isnt showing up.
PHP Code:
<input type="text" value="<?php echo $row['MiddleName'];?>" name="middlename" />
How do I fix that?
__________________
MY MSN: Sith717@Hotmail.com
PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 08:33 PM Re: If blank then dont Update
orionoreo's Avatar
Ultra Talker

Posts: 261
Name: Jerry
Trades: 0
you didn't even call $row yet

PHP Code:
$row mysql_fetch_array(mysql_query("SELECT * FROM 'testing' WHERE id='$id'")); 
and btw your method, somebody can still set text fields to null and empty out your columns
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 08:54 PM Re: If blank then dont Update
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
Thanks here is my final code:

PHP Code:
<html>
<body>
<?php
if (isset ($_POST['submit'])) // if the form was submitted, display their name
{
require_once (
'inc/config.php');
$firstname mysql_real_escape_string ($_POST['firstname']);
$lastname mysql_real_escape_string ($_POST['lastname']);
$middlenamemysql_real_escape_string ($_POST['middlename']);
$id 2;  
$sql mysql_query ("
   UPDATE `testing` SET 
      `FirstName` = '"
.$firstname."', 
      `LastName` = '"
.$lastname."',
      `MiddleName` = '"
.$middlename."' 
   WHERE `id` = '"
.$id."'
"
)
OR die (
mysql_error());
echo 
"hello";
}

require_once (
'inc/config.php');
$query "SELECT * FROM testing";
$result mysql_query($query) or die(mysql_error());
while (
$row mysql_fetch_array ($result))
{
?>



<form action="<?php echo $_SERVER['PHP_SELF'?>" method="post">
Firstname: <input type="text" value="<?php echo $row['FirstName'];?>" name="firstname" /><br>
Lastname: <input type="text" value="<?php echo $row['LastName'];?>" name="lastname" /><br>
middlename: <input type="text" value="<?php echo $row['MiddleName'];?>" name="middlename" /><br>
<input type="submit" id="submit" name="submit" value="Submit!">
</form>
<?php
}
?>

</body>
</html>
Now I have a new problem:

I want to echo what is currently in the database but it wont show up
PHP Code:
<?php
// show errors if any
error_reporting(E_ALL);
ini_set('display_errors''1');

// require a file
require_once ('inc/config.php');

// select row from table
$query "SELECT * FROM testing";

// check if is valid if it is then show results if not then die
$result mysql_query($query) or die(mysql_error()); 
while (
$row mysql_fetch_array ($result)) 
{
?>
First Name:
<?php $row['FirstName']; ?>
<br>Last Name:
<?php $row['LastName']; ?>
<br>Middle Name:
<?php $row['MiddleName']; ?>
<?
}
?>
__________________
MY MSN: Sith717@Hotmail.com
PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-05-2009, 02:49 AM Re: If blank then dont Update
lizciz's Avatar
Ultra Talker

Posts: 330
Name: Mattias Nordahl
Location: Sweden
Trades: 0
You forgot to actually print it.
PHP Code:
<?php $row['Firstname']; ?>
should be
PHP Code:
<?php echo $row['Firstname']; ?>
__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 11-05-2009, 01:00 PM Re: If blank then dont Update
Skilled Talker

Posts: 61
Name: John
Trades: 0
Shorthand:
PHP Code:
<?= $row[0?>
Note: "<?=" != "<?php"
Envision_frodo is offline
Reply With Quote
View Public Profile
 
Old 11-05-2009, 04:08 PM Re: If blank then dont Update
chrishirst's Avatar
Super Moderator

Posts: 22,237
Location: Blackpool. UK
Trades: 0
depends on how the server is set up.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-05-2009, 05:57 PM Re: If blank then dont Update
Skilled Talker

Posts: 61
Name: John
Trades: 0
Sorry, should've mentioned... In order to use the shorthand method you must have the "short_open_tag" option enabled in your php.ini file. Also, the use of shorthand is highly discouraged given the decreased portability.

That doesn't mean you can't still love it. As a pure text-only coder it just "feels" cleaner to me.
Envision_frodo is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to If blank then dont Update
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.14496 seconds with 13 queries