Jump to content

Recommended Posts

Hello!

I made a real big user-table with many columns.

So I wan't to make a function that sets a line to the default.

For instance:

I got a table like

user_id, user_name (default = ""), user_password (default = ""), user_getpms (default = 1)

 

So how can I update a line where user_id = 2 (for example) without writing every column.

But the user_id should be the same like it was before.

 

Do anyone understand me?

Link to comment
https://forums.phpfreaks.com/topic/46849-solved-set-all-to-default/
Share on other sites

I am not sure but I think default values apply only during inserts... consider this:

 

DELETE FROM table_name WHERE user_id = $id;

INSERT INTO table_name SET user_id = $id;

 

The default values should be set during the insert so make sure all your column definitions have appropriate default values.

Not sure if your looking for something like this but it was fun to create

 

<?php

$theStuff =array("Name" => "NewName", "Age" = > "15");
SetToDatabase($theStuff);


function SetToDatabase($theValue)
{
$table = "Test";

//Get list of Fields
$result = mysql_query("DESCRIBE `".$table."`");
$Data = array();
while ($row = mysql_fetch_array($result,MYSQL_NUM))
{
	if( isset($theValue[$row[0]]) )
	{
		$Data[$row[0]] = $theValue[$row[0]];
	}else{
		$Data[$row[0]] = ""; //empty unset values
	}
}

//Insert
//	$Fields = array_keys($Data);
//	$TheSet = "(".implode(",",$Fields).")";
//	$TheValue = "(".implode(",",$Data).")";
//	$SQLq  = "INSERT INTO $table $TheSet VALUES $TheValue;"

//Update
$UPDATE = "UPDATE $table SET ";
foreach($Fields as $F => $V)
{
	$UPDATE .= "`$F` = '$V'";
}
$SQLq  = $UPDATE." " //<-- Where Blar

$result = mysql_query($SQLq );
}

?>

 

it probably has a few bugs as its written from the top of  myhead but .. yeah

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.