Welcome! Log In Create A New Profile

Advanced

DATABASES

Posted by 30627303 Naaz 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
DATABASES
September 13, 2013 04:12PM
Starting with my Portfolio. Im reading the requirements and need to understand the different tables that is required. In order for me to create a login page I require a database for it. Do I create a separate tablesthan the ones given meaning should I have four tables
avatar
Mac
Re: DATABASES
September 13, 2013 04:37PM
The login can be manged at code level. That said, nothing stops you from creating a table (you should be using the same DB throughout, so just add a table) to manage logins. Here it is a bit of an overkill, since it is just one user you need to check.
Re: DATABASES
September 15, 2013 07:06PM
This one is bad. I am in prison and internet access is a definite NO. But i guess as from today i shall be online "underground". I have created the database with three(3) tables though no data is loaded yet. As for the connection.php page which must be included in all the pages, i can't get it right. I receive a blank page after trying to log the admin in. I have created the connection page with all the details. N.B I did create the user admin with the admin password.
avatar
Mac
Re: DATABASES
September 16, 2013 07:16AM
The connection page is a very simple page with db connection details, and if i connects, you can echo that a connection has been established . If it is not working then clearly there is a mistake.
Re: DATABASES
September 17, 2013 09:54AM
Hi,
Do one have to create a different connect.php for every table or will I be able to create one connect.php and just list all the tables in it.
avatar Re: DATABASES
September 17, 2013 10:26AM
You connect to the database, not individual tables.
So, you only need one connect.php.

You specify the required table/s in your SQL query.

eg: SELECT * FROM tableName
Re: DATABASES
September 17, 2013 10:34AM
Hi, Thanks

Guess I will have to change it then. I listed tablename as variable and it seems to work fine. I am busy with the the insert.php and when looking at the phpAdmin it has been updating with the information I captured onto the form.
avatar Re: DATABASES
September 17, 2013 09:16PM
From the login page you would utilize an "include" statement to keep your connection up for the queries (remember to close it though) that way you don't need to keep adding the whole code for it. After connecting to the database for authentication you would need to use a session variable to keep track that your user is logged in smile hope this helps
avatar Re: DATABASES
September 18, 2013 03:45PM
i have four tables on my database is it possible to use the date for the date field instead of varchar
avatar
Mac
Re: DATABASES
September 18, 2013 03:54PM
Yes, absolutely. Sometimes I feel guilty about my (small steps) approach... this is only the 4th time in 4 years that this question is asked. Good for you. Remember, I do not have access to your DB structure - other than what I pick up in your code. 1st requirement - it must work.
Re: DATABASES
September 26, 2013 10:37AM
Hi guys

Always when I try to connect to my database i get this error message, i dont know what it means?

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\ICT2613_Portfolio_Sem2_2013_MalulekeD_37024892\loginn.php on line 38

The code on line 38
$selected = mysqli_select_db("portfolio",$dbhandle)

Please Assist

Thx D
avatar
Mac
Re: DATABASES
September 26, 2013 10:58AM
The connection comes before the DB name
Re: DATABASES
September 27, 2013 07:36AM
Hi

Thx man, its working now

hot smiley
Re: DATABASES
October 04, 2013 06:50PM
Thank you! A programmer tries all posibilities he/she can think of before asking. In the insert page,after inserting a new player, the player's details are missing in the list _players page BUT the last column of Edit Delete is added. I seem to loose data along the way.
Language: PHP
<?php   //Inserting a new player';s information from the form to the table   //Check if still same session session_start(); if (!(isset($_SESSION[';login';]) && $_SESSION[';login';] != ';';)) {     include("connect.php");       if ($db) { //Test the database connection $errors_array = array(); session_start(); form_data(); if (isset($_POST[';Submit';])) //check if submit button was clicked { //Assigning form names to short variables $fname = $_POST[';player_name';]; $sname = $_POST[';player_surname';]; $cnumber = $_POST[';player_contact_num';]; $email = $_POST[';player_email';]; $pos = $_POST[';player_position';]; $uname = $_POST[';player_username';]; $pword = $_POST[';player_password';];   check_data(); //Verify the submitted data   if (count($errors_array) == 0) //Check if there were any errors from check_data function { $query = "INSERT INTO playerregistration(player_name, player_surname, player_contact_num, player_email, player_position, player_username, player_password) VALUES( ';$fname';, ';$sname';, ';$cnumber';, ';$email';, ';$pos';, ';$uname';, ';$pword';)";   $result = mysql_query($query); if ($result) //Check if the record was indeed inserted { echo "Player added successfully.";     } else { echo "Failed to add a player.";   } mysql_close($connection);   } else { echo "Please correct the following errors: <br>"; show_errors();   } mysql_close($connection); } else { $fname = ""; $sname = ""; $cnumber = ""; $email = ""; $pos = ""; $uname = ""; $pword = ""; }   }       else { echo ("Could not connect to the database"); } } else { header("Location: login.php"); }             // This function checks the data entered by the user function check_data() { global $errors_array;       if ($_POST["fname"] == "") { $errors_array[] = "<font color=';red';>Enter the name</font>"; } if ($_POST["sname"] == "") { $errors_array[] = "<font color=';red';>Enter the surname</font>"; } if ($_POST["cnumber"] == "") { $errors_array[] = "<font color=';red';>Enter the contact number</font>"; } //if (strcmp($_POST["cnumber"], strval(intval ($_POST["cnumber"])))) { //$errors_array[] = "<font color=';red';>Please enter an integer</font>"; //} if ($_POST["email"] == "") { $errors_array[] = "<font color=';red';>Enter the email</font>"; } if ($_POST["pos"] == "") { $errors_array[] = "<font color=';red';>Enter the position</font>"; } if ($_POST["uname"] == "") { $errors_array[] = "<font color=';red';>Enter the username</font>"; } if ($_POST["pword"] == "") { $errors_array[] = "<font color=';red';>Enter the password</font>"; }     } // The errors during the processing is captured by this function then displayed function show_errors() { global $errors_array;   foreach ($errors_array as $err) { echo $err, "<br />"; } }       // The form to collect the data function form_data() { //Assigning the form values to the variables $fname = isset ($_REQUEST[';fname';]) ? $_REQUEST[';fname';] : ""; $sname = isset($_REQUEST[';sname';]) ? $_REQUEST[';sname';] : ""; $cnumber = isset($_REQUEST[';cnumber';]) ? $_REQUEST[';cnumber';] : ""; $email = isset($_REQUEST[';email';]) ? $_REQUEST[';email';] : ""; $pos = isset($_REQUEST[';pos';]) ? $_REQUEST[';pos';] : ""; $uname = isset($_REQUEST[';uname';]) ? $_REQUEST[';uname';] : ""; $pword = isset($_REQUEST[';pword';]) ? $_REQUEST[';pword';] : "";   echo "<div align=';center';>"; echo "<form method=';post'; action=';insert.php'; >"; echo "<table cellpadding=';5'; bgcolor=';aqua'; border>"; //table to hold the data in the form aligned echo "<td colspan=';2';>&nbsp&nbsp&nbsp&nbspFill in the details:</td></tr>";   echo "<tr>"; echo "<td>Name: </td>"; echo "<td><input name=';fname'; type=';text'; value=';", $fname, "';></td></tr>"; //variable to hold the nasme to persist during processing echo "<br />";   echo "<tr>"; echo "<td>Surname: </td>"; echo "<td><input name=';sname'; type=';text'; value=';", $sname, "';></td></tr>"; echo "<br />";   echo "<tr>"; echo "<td>Contact number: </td>"; echo "<td><input name=';cnumber'; type=';text'; value=';", $cnumber, "';></td></tr>";   echo "<br />";   echo "<tr>"; echo "<td>Email: </td>"; echo "<td><input name=';email'; type=';text'; value=';", $email, "';></td></tr>"; echo "<br />";   echo "<tr>"; echo "<td>Position: </td>"; echo "<td><input name=';pos'; type=';text'; value=';", $pos, "';></td></tr>"; echo "<br />";   echo "<tr>"; echo "<td>Username: </td>"; echo "<td><input name=';uname'; type=';text'; value=';", $uname, "';></td></tr>"; echo "<br />";   echo "<tr>"; echo "<td>Password: </td>"; echo "<td><input name=';pword'; type=';text'; value=';", $pword, "';></td></tr>"; echo "<br />";   echo "<tr >"; echo "<td align=';center'; colspan=';2';><input type=';submit'; name = ';Submit'; value=';Insert';></td></tr>"; echo "</table>"; echo "</form>"; //end of the form echo "</div>"; }   ?>
avatar Re: DATABASES
October 04, 2013 09:40PM
Does your script display -

"Player added successfully." ?


You display your form, and immediately thereafter check if $_POST Submit is set.
When Submit is clicked, the form Action is executed, not any code following the form.

It is therefore unlikely that any code in your
Language: PHP
if (isset($_POST[';Submit';]))
condition is ever executed, or if it is, then not in the sequence that you expect.

You need to put your
Language: PHP
if (isset($_POST[';Submit';]))
code before you display the form.
Re: DATABASES
October 05, 2013 09:18PM
Yes the script does display

"Player added successfully."

I tried putting the form inside the
Language: PHP
if (isset($_POST[';Submit';]))

but now the form does not display at all. Blank screen. When i check if the player was added, it shows that it added only the last column(Action, Edit and Delete options) BUT not the data from the form.
avatar Re: DATABASES
October 05, 2013 09:53PM
You must display the form right at the end, after the if else construct (after the last closing bracket) - not within any if (isset()) construct.

You need to bear in mind how PHP and HTML operates.
PHP is interpreted and processed on the server, only the HTML is served to the end user computer.

The HTML is interpreted and processed by the users browser. When the form is displayed / enabled , there is no PHP present.
avatar Re: DATABASES
October 05, 2013 10:45PM
You could also substantially improve the logic of your script.

There is no need to put the login check in an if else construct.
You could just do something like this at the top of your script -

if (not logged in)
redirect to login page;

The if else serves no purpose.


You also connect to the database even if it is not necessary to do so.
You only need to connect to the database if the $_POST isset condition is true, and the data validates.

There is no purpose in connecting to the database if there is no data, or if the data does not validate.


There is a lot of unnecessary repetition of code in your check_data() function.

You could do all the font styling in one line in the show_errors() function.
Re: DATABASES
October 08, 2013 07:22PM
Yes i did try out putting the form outside the if's yet same results. I've improved on my logic BUT still get same results.

Language: PHP
<?php   //Inserting a new player';s information from the form to the table   //Check if still same session session_start(); if (!(!(isset($_SESSION[';login';]) && $_SESSION[';login';] != ';';))) { header("Location: login.php"); }   $errors_array = array(); session_start();     if (isset($_POST[';Submit';])) //check if submit button was clicked {   //Assigning the submitted form names to short variables $fname = $_POST[';player_name';]; $sname = $_POST[';player_surname';]; $cnumber = $_POST[';player_contact_num';]; $email = $_POST[';player_email';]; $pos = $_POST[';player_position';]; $uname = $_POST[';player_username';]; $pword = $_POST[';player_password';];   check_data(); //Verify the submitted data   if (count($errors_array) == 0) //Check if there were any errors from check_data function { include("connect.php"); if (!$db) { //Test the database connection echo ("Could not connect to the database"); } $query = "INSERT INTO playerregistration(player_name, player_surname, player_contact_num, player_email, player_position, player_username, player_password) VALUES( ';$fname';, ';$sname';, ';$cnumber';, ';$email';, ';$pos';, ';$uname';, ';$pword';)";   $result = mysql_query($query); if ($result) //Check if the record was indeed inserted { echo "Player added successfully.";     } else { echo "Failed to add a player.";   } mysql_close($connection);   } else { echo "Please correct the following errors: <br>"; show_errors(); }   }                                 // This function checks the data entered by the user function check_data() { global $errors_array;       if ($_POST["fname"] == "") { $errors_array[] = "<font color=';red';>Enter the name</font>"; } if ($_POST["sname"] == "") { $errors_array[] = "<font color=';red';>Enter the surname</font>"; } if ($_POST["cnumber"] == "") { $errors_array[] = "<font color=';red';>Enter the contact number</font>"; } //if (strcmp($_POST["cnumber"], strval(intval ($_POST["cnumber"])))) { //$errors_array[] = "<font color=';red';>Please enter an integer</font>"; //} if ($_POST["email"] == "") { $errors_array[] = "<font color=';red';>Enter the email</font>"; } if ($_POST["pos"] == "") { $errors_array[] = "<font color=';red';>Enter the position</font>"; } if ($_POST["uname"] == "") { $errors_array[] = "<font color=';red';>Enter the username</font>"; } if ($_POST["pword"] == "") { $errors_array[] = "<font color=';red';>Enter the password</font>"; }     } // The errors during the processing is captured by this function then displayed function show_errors() { global $errors_array;   foreach ($errors_array as $err) { echo $err, "<br />"; } }       // The form to collect the data function form_data() { //Assigning the form values to the variables $fname = isset ($_REQUEST[';fname';]) ? $_REQUEST[';fname';] : ""; $sname = isset($_REQUEST[';sname';]) ? $_REQUEST[';sname';] : ""; $cnumber = isset($_REQUEST[';cnumber';]) ? $_REQUEST[';cnumber';] : ""; $email = isset($_REQUEST[';email';]) ? $_REQUEST[';email';] : ""; $pos = isset($_REQUEST[';pos';]) ? $_REQUEST[';pos';] : ""; $uname = isset($_REQUEST[';uname';]) ? $_REQUEST[';uname';] : ""; $pword = isset($_REQUEST[';pword';]) ? $_REQUEST[';pword';] : "";   echo "<div align=';center';>"; echo "<form method=';post'; action=';insert.php'; >"; echo "<table cellpadding=';5'; bgcolor=';aqua'; border>"; //table to hold the data in the form aligned echo "<td colspan=';2';>&nbsp&nbsp&nbsp&nbspFill in the new player';s information:</td></tr>";   echo "<tr>"; echo "<td>Name: </td>"; echo "<td><input name=';fname'; type=';text'; value=';", $fname, "';></td></tr>"; //variable to hold the nasme to persist during processing echo "<br />";   echo "<tr>"; echo "<td>Surname: </td>"; echo "<td><input name=';sname'; type=';text'; value=';", $sname, "';></td></tr>"; echo "<br />";   echo "<tr>"; echo "<td>Contact number: </td>"; echo "<td><input name=';cnumber'; type=';text'; value=';", $cnumber, "';></td></tr>";   echo "<br />";   echo "<tr>"; echo "<td>Email: </td>"; echo "<td><input name=';email'; type=';text'; value=';", $email, "';></td></tr>"; echo "<br />";   echo "<tr>"; echo "<td>Position: </td>"; echo "<td><input name=';pos'; type=';text'; value=';", $pos, "';></td></tr>"; echo "<br />";   echo "<tr>"; echo "<td>Username: </td>"; echo "<td><input name=';uname'; type=';text'; value=';", $uname, "';></td></tr>"; echo "<br />";   echo "<tr>"; echo "<td>Password: </td>"; echo "<td><input name=';pword'; type=';text'; value=';", $pword, "';></td></tr>"; echo "<br />";   echo "<tr >"; echo "<td align=';center'; colspan=';2';><input type=';submit'; name=';Submit'; value=';Insert';></td></tr>"; echo "</table>"; echo "</form>"; //end of the form echo "</div>"; } form_data(); //Display the form to enter the new information   ?>



Looks like, a record is indeed created in the database BUT with empty fields, when I retrieve the data from the list_players.php page,only the last part(Edit Delete) is visible with other fields empty.

Please help me understand the following:

PHP is strictly for processing data at the server level?
The program flow in PHP,how is the code processed?
From the forms,PHP is only interested with the data in the form (text fields etc), not how the form is presented?
avatar Re: DATABASES
October 08, 2013 08:14PM
Here is your error -

Your $_POST array references non existent values.

Language: PHP
$fname = $_POST[';player_name';]; $sname = $_POST[';player_surname';]; // This should be $_POST[';sname';]; $cnumber = $_POST[';player_contact_num';]; $email = $_POST[';player_email';]; $pos = $_POST[';player_position';]; $uname = $_POST[';player_username';]; $pword = $_POST[';player_password';];

The names used in your $_POST array must be identical to the input names in your form.

Language: PHP
echo "<td><input name=';sname'; type=';text'; value=';", $sname, "';></td></tr>"; // Must use this name "sname" in your $_POST array.
avatar Re: DATABASES
October 08, 2013 08:23PM
Quote

Please help me understand the following:

PHP is strictly for processing data at the server level?
The program flow in PHP,how is the code processed?
From the forms,PHP is only interested with the data in the form (text fields etc), not how the form is presented?


PHP is fully interpreted on the server. No PHP code is served to the client, only the HTML, JavaScript etc is served.

PHP program flow on the server is similar to a normal interpreted language, except that the output is served to the client computer in a format that can be interpreted by the users browser.

Presentation is done by HTML / CSS, not PHP. There is no PHP present in the users browser.
Suggestion - run your script, when the form is displayed, right-click and then "View Source" (or similar).
You will see the code that is actually served to the browser - compare it to your script.

So, it is a 2 stage process -

The PHP is interpreted on the server, and the resulting HTML served to the users browser.
The HTML is then interpreted by the users browser.
avatar Re: DATABASES
October 08, 2013 10:08PM
It would be far simpler to do away with the global errors_array and do the following -
You will eliminate all problems to do with scope, visibility of variables, global etc. by doing so.

Get your check_data() function to return the error list as a string variable.
You would need to omit the font styling from check_data(), and do it in show_errors().

You then need to check the length of the string to see if there are any errors.

Change the show_errors() function to accept the error string as a parameter.
You then just need to echo the string, no need for an array or loop.

The use of global variables is considered to be poor programming practice, and you should generally avoid it where possible.
avatar Re: DATABASES
October 08, 2013 10:47PM
There is still a logical error in your script.

You continue to execute your SQL query even if the connection to the database fails.
That would be an appropriate place to use an if ... else construct.
avatar Re: DATABASES
October 09, 2013 01:02PM
guys my problem is uploading the database unto the hosting area how do we do it
avatar Re: DATABASES
October 09, 2013 01:51PM
You can either -

Use phpMyAdmin to create a database.

Or, you can export / import your database file.

There are at least 3 possible methods (one of which involves phpMyAdmin).

Have a look here -

http://www.itworld.com/it-management/359857/3-ways-import-and-export-mysql-database
Re: DATABASES
October 09, 2013 01:51PM
@Charlton - Firstly whatsup with your name?
Seriously!!!

you can create a sql Script in mySQL
then you upload it,
and if you create a db I think there is an option to do it from the script.
another option is recreating it, but takes way too much work.
Sorry, only registered users may post in this forum.

Click here to login