/    Sign up×
Community /Pin to ProfileBookmark

Get default value fron SQL

Hi

I would like to know BEFORE I SAVE A NEW ENTRY THAT HE USER IS FILLING how I can it shows the default value thatis set in SQL db.

I have set in my SQL as follows:

FIELD: date1

TYPE: DATE

NULL: NULL

Default: 0000-00-00

The script is:

[code=php]

<?

if ($id) {

$sql2=”select * from &#8220;table name&#8221; where id=’$id’ limit 1 “;

$result=mysql_db_query(“database “,$sql2);

$regist=mysql_fetch_array($result);

$id=$regist [“id”];

(other)

(other)

$date=$regist [“date1”];

}

?>

<table>

<tr>

<td width=”36″ align=”center”><input name=”data1″ type=”text” id=”data1″ value='<? echo”$data1″ ?>’ size=”5″ /></td>

</tr>

</table>

[/code]

The purpose is to appear in the field already 0000-00-00 to show the user the way he must enter the date, instead of having a hint (000-00-00) on the side or on the bottom of the value field.

I hope I have made myself understood.

Thanks a lot

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@TrollsplattererJul 22.2010 — I guess something like this would work if the new record with the given ID has indeed already been created with a default date-value. Otherwise you'll just get an empty resultset and, in your code, an error.

Why would you want to get this format from the database, rather than putting the value fixed in the HTML-code? It's not like this default value is going to change much, is it? And if the format in the fill-in is not the same as the database, you can still convert it to the right date format when posting the form.

So I would just make it like this:

<input name="data1" type="text" id="data1" value='0000-00-00' size="5" />

If you ensist on using the database as format source, you must create the new record first.
Copy linkTweet thisAlerts:
@tirnaJul 23.2010 — I assume you mean the default database value for the column the user entered data will be inserted into.

If so then in MySQL, and other databases will be similar, you can query the database schema if you don't know what the default value is.

In the schema, there is a table call "columns" which stores all the attributes for each column in each table. One of the columns within the table called columns is column_default.

Column_default is the column you need to query to get the default value for a particular column in a table.
×

Success!

Help @JoseSilva spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...