/    Sign up×
Community /Pin to ProfileBookmark

Add a record to phpMyadmin database

Hi, I have recently created a database on phpMyadmin and added php code to my website to read from the database. The code is below:

[code=php]
<?PHP

$user_name = “Myusername”;
$password = “Mypassword”;
$database = “Mydatabase”;
$server = “Myserver”;

$db_handle = mysql_connect($server, $user_name, $password);

$db_found = mysql_select_db($database, $db_handle);

if($db_found){

$SQL = “SELECT * FROM Wavelength”;
$result = mysql_query($SQL);

while ( $db_field = mysql_fetch_assoc($result) ) {

print $db_field[‘Telescope ID’] . “<BR>”;
print $db_field[‘Telescope Name’] . “<BR>”;
print $db_field[‘Acronym’] . “<BR>”;
print $db_field[‘Wavelength’] . “<BR>”;
print $db_field[‘Cost’] . “<BR>”;
print $db_field[‘Launched’] . “<BR>”;
print $db_field[‘Mission Length’] . “<BR>”;

}
mysql_close($db_handle);
}
else{
print”Database NOT Found”;
mysql_close($db_handle);
}

?>
[/code]

This successfully displayed my database on my website so the next stage was to add another entry into the database using the following code:

[code=php]
<?PHP

$user_name = “Myusername”;
$password = “Mypassword”;
$database = “Mydatabase”;
$server = “Myserver”;

$db_handle = mysql_connect($server, $user_name, $password);

$db_found = mysql_select_db($database, $db_handle);

if($db_found){

$SQL = “INSERT INTO Wavelength (Telescope Name, Acronym, Wavelength, Cost, Launched, Mission Length)
VALUES ($’James Webb’,$’JWST’,$’Infra-red’,$’10B’,$’1998-10-6′,$’10’)”;

$result = mysql_query($SQL);

mysql_close($db_handle);

print”Records added to the database”;
}
else{
print”Database NOT Found”;
mysql_close($db_handle);
}

?>
[/code]

This seemed to be successful, on my website it said “Records added to the database” but when I opened up PHPMyadmin no new entry was there. The database is a space telescopes database with a table name of Wavelength and 7 fields. For the last few months I have learnt how to write enough code to launch a website and I am continuing to learn new skills, I have become stuck with this problem and have failed to find any solutions. I would greatly appreciate help from someone with this problem.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmAug 20.2014 — 1 - You are inserting records into your MySQL(?) database, not your phpadmin database. Semantics but in the IT world you have to learn to be specific. Phpadmin is merely a tool for accessing your MySQL(?) database, much like php is a tool to program web sites.

2 - Please post code using the proper tags for this forum. If you read the posting rules you would know that but to help you out: You must use < code > and </ code > to start and end your block of code (without the spaces that I added here).

3 - If you would read the php manual (mentioned in the top of the forum as an important resource) you would find that the MySQL_* functions are all deprecated and should not be used. Time to change and learn something new. MysqlI and PDO are the usual db extensions that are available. PDO is the preferred one for many.

4 - Assuming that you are asking for help with the second script I can tell you the problem is your choice of field names for your table. It is generally not a good idea to use spaces in your field names because they will only give you trouble forever after. Use an underscore or simply don't use spaces. To solve your problem you must use backticks in your query statement (key to the left of '1'). Backticks must be used whenever you have a name such as this or when the name is a reserved word.

5 - [B]Always Use PHP Error Checking[/B] when you are developing new code. Put the following at the beginning of your php scripts.
<i>
</i>error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');


(this code is properly wrapped in the code tags.)

Good luck and keep on trucking!
Copy linkTweet thisAlerts:
@cha99kepauthorAug 21.2014 — Thanks very much for your quick and detailed reply, I clearly have a lot more to learn.
×

Success!

Help @cha99kep 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.6,
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,
)...