Greetings everyone

i'm new to php and this is what i want to do if you can help me , i have a DB on my web server and there are two column one for the name of the product and the other for the id of the product , i added text box but i want to know how can i take the text written there and use it to create new column every time i click a button e.x when i write 1/1/2010 in the text box and click the button new column well be created in the DB also i wish that all the records of the products become 0 under that column so finally this what the DB will look like

proudcts ID "the new column i added"

milk 12 0
sugar 13 0
raise 11 0

i' already connected to DB and i made the text box and the button this is what i make so far

<form id="form1" name="form1" method="post" action="">
  <p>
    <label>date
      <input type="text" name="date" id="date" />
    </label>
  </p>
  <p>
    <label>add new date
      <input type="submit" name="add" id="add" value="Submit" />
    </label>
  </p>
</form>
<p>&nbsp;</p>
<?php 

$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "le";


@mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect"); 
@mysql_select_db("$db_name") or die ("No database found");
echo"you are connected to the DB";
?>

i wish someone help me , thank you "hint i'm going to add only dates like 1/1/2011 & 2/1/2011....

Just for clarification's sake, you want to add another Field/Column onto you table and NOT add another row?

Adding another field/column would mean that you will create another column with a data type, for example.. the fields in you table would be ID, ProductName, Quantity and if you were to add another field to that like Price, your table would have an Extra field for you to Enter Data into. Adding fields would be the same as querying your database very much like a select statement but with the use of the ALTER statement.

mysql_query("ALTER TABLE table_name ADD COLUMN 'col1' INT(10) DEFAULT 0");
//INT is the datatype like varchar or char or text etc

//if you already have data saved in it and you want to do a mass update so that all the data for that field would be 0
mysql_query('UPDATE table_name SET col1 = 0');
//no where statement

If you just want to add a row to the table use an INSERT statement instead.

thank You Sir it works !

Hello, I need help too. If in my form i put this,

<td><input type="text" name"Example" id="Example"></td>

and my sql what would i put? Mine is,

$insertQuery = "ALTER TABLE atable ADD column '$example' INT";

this is the error shown, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' INT' at line 1. I tried many ways but cannot put in the correct syntax. Please help, Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.