Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add edit delete new tables in database

Status
Not open for further replies.

AbbasAkhtar

Programmer
Dec 16, 2002
25
GB
Hi again i need some help with mysql and php again, this is my problem:

Is it possible to retreive only 1 line in a table in a database for example,

if i had a database called mydb and a table within called details, and it's like this

First Name Age

Abbas Akhtar 17
John Smith 23


can i do something to print out the first data in the table details which would be my name and my age

and something to print out the second data in the table which would be john smith whos age is 23,

also, is there any way to count all the datas in a tables, so the count for mine above would be 2 because only 2 datas exists,

do u get wot i mean, i can't explain it any other way.


also, at the top, of my page i use this:

//get values/variables if available
$v = $_POST['agee'];
$v2 = $_POST['namee'];

this works, but if nothing has been posted or the page is loaded for the first time and nothing submitted, it displays on the html page:

Notice: Undefined index: agee in C:\Program Files\Apache Group\Apache2\htdocs\db2.php on line 3

Notice: Undefined index: namee in C:\Program Files\Apache Group\Apache2\htdocs\db2.php on line 4

but if i submit after this once, it wont display it no more, is there any way i can detect if there are any $_POST's available so it wont show the error if nothing has been posted, could someone explain how to fix this,

also, in the php file, i want to know how i can clear the table details in mydb and create it again so i want 2 buttons clear all and create new, this is the code im using but im getting errors ? any ideas

<?php
//get values/variables if available
$v = $_POST['agee'];
$v2 = $_POST['namee'];

// Establish a connection to the MySQL DBMS
$connection = mysql_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;abbas&quot;);

// Use the mydb database
mysql_select_db(&quot;mydb&quot;, $connection);

//if values are greater or less than nothing then do this
if ($v and $v2 <> '')
{
mysql_query (&quot;INSERT INTO details VALUES ('$v2','$v')&quot;);
}

// Run a query through the connection
$result = mysql_query(&quot;SELECT name, age FROM details&quot;, $connection);

// Fetch each row of the results into an array $row
echo &quot;<h2>Database details</h2><p>&quot;;

//reconnect to the database to start again from the beginning
mysql_select_db(&quot;mydb&quot;, $connection);
$result = mysql_query(&quot;SELECT name, age FROM details&quot;, $connection);

while ($row = mysql_fetch_array($result))
{
echo &quot;First Name: {$row[&quot;name&quot;]} Age: {$row[&quot;age&quot;]}\n<br>&quot;;
}

echo &quot;</select><p>&quot;;

//set values/variables to nothing

$result = mysql_query(&quot;SELECT name, age FROM details&quot;, $connection);


echo &quot;Delete Details:<p><select name=\&quot;select\&quot; size=\&quot;10\&quot;>&quot; ;

while ($row = mysql_fetch_array($result))
{
echo &quot;<option>&quot;;
echo &quot;{$row[&quot;name&quot;]}\n&quot;;
echo &quot;</option>&quot;;
}

echo &quot;</select><p>&quot;;
?>

<form name=&quot;clear_all&quot; action=&quot;
<?
mysql_connect('localhost','root','abbas');
@mysql_select_db('mydb') or die( 'Unable to select database');

$query = 'DROP TABLE `details` '; ;
mysql_query($query);

mysql_close();
?>
&quot;>
<p>
<input name=&quot;clear&quot; type=&quot;button&quot; id=&quot;clear&quot; value=&quot;Clear Details&quot;>
</form>


<form name=&quot;newtable&quot; action=&quot;

mysql_connect('localhost','root','abbas');
@mysql_connect_db('mydb');
mysql_query ('CREATE TABLE details (name VARCHAR(255), age VARCHAR(2))');
mysql_close();

&quot;>
<input type=&quot;button&quot; name=&quot;new_db_table&quot; id=&quot;new_table&quot; value=&quot;Create New Table&quot;>
</form>

<?php
$v == '';
$v2 == '';
?>


<p>
<form name=&quot;add_details&quot; method=&quot;post&quot; action=&quot;db2.php&quot;>
Enter Name: <input type=&quot;textfield&quot; name=&quot;namee&quot;> <br>
Enter Age: <input type=&quot;textfield&quot; name=&quot;agee&quot;> <br>
<input type = &quot;submit&quot; name=&quot;submit&quot; value=&quot;Add to Database&quot;>
</form>

thanks.
 
Question 1:

You can select only x rows from a table using a query like:

Code:
SELECT * FROM tablename LIMIT 2

(FYI: However, database servers don't think of a datbase table as an ordered list -- database servers operate on unordered sets of data. As such, depending on the indexing you're using in the table, the order in which you inserted the records does not have to be the order in which they will be returned. This is where the query clauses &quot;WHERE&quot; and &quot;ORDER BY&quot; come in.

Question 2:
If you do not submit data to the script, then there is nothing in $_POST. One way around this is to test for the presence of the field-data you're looking for. A couple of ways to do it:

Code:
if (array_key_exists('v'. $_POST))
{
   //Do stuff
}
else
{
   // Do other stuff
}

or

Code:
if (is_set ($_POST['v']))
{
   //Do stuff
}
else
{
   // Do other stuff
}


Question 3:
Insufficient data for a meaningful answer. What errors are you getting where in your code? Want the best answers? Ask the best questions: TANSTAAFL!
 
Problem #1
You retreive the data using, for example:

$link = mysql_connect($host,$user,$pass);
mysql_select_db('mydb',$link);
$SQL = &quot;SELECT * FROM mydb&quot;;
$result = mysql_query($SQL,#link);

The $result is a resource identifier which you can use to get more information about the result set.
$howMany = mysql_num_rows($result); # how many rows in the result

To position the internal row pointer you can use:
mysql_data_seek($result, $rowNumber);

The only thing to mention here is that the first row in the result set has index 0 (zero).

Problem #2
You can check for the existence of a variable with isset();
if (isset($_POST['blah'])){
$v = $_POST[blah];
}

Problem #3

I see:
<form name=&quot;newtable&quot; action=&quot;

mysql_connect('localhost','root','abbas');
@mysql_connect_db('mydb');
mysql_query ('CREATE TABLE details (name VARCHAR(255), age VARCHAR(2))');
mysql_close();

&quot;>

You are mixing up HTML and PHP.
The HTML form action attribute tells the HTML form where and how to send data when the form is submitted. It does not &quot;do&quot; any PHP code.
PHP is pre-hypertext-processor, so all the code is executed before the HTML is sent to the browser.

I suggest that you add some statements to the top of the PHP file to handle the submission. Also, submit the form to itself and give the SUBMIT buttons specific names e.g.
<input type=&quot;submit&quot; name=&quot;emptyTable&quot; method=&quot;post&quot; action=&quot;....

# check if the dropDb button was pressed
if ($_POST[emptyTable]!=''){
// mysql directives here
}

Instead of dropping the table, just empty it:
$SQL = &quot;DELETE FROM myTable&quot;;
This way you need not create a new table.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top