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!

total newbie need help

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I have two files one is a form and other is suppose to write the data to MySQL
I have downloaded HeidiSQL and created a table and added one row to it so I know the database is there and working. It's on a hosted site.
this is a demo I saw on you tube. Can anyone see anything wrong with this.

this files name is demo.php
Code:
<?php
define('DB_NAME','pcsuppor_forms1')
define('DB_USER','root')
define('DB_PASSWORD','mypassword')
define('DB_HOST','localhost')
echo
$link=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD)
IF (!$link {
	die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
	die('Can\'t use '. DB_NAME . ': ' . mysql_error());
}


$value = $_POST['input1'];

$sql = "INSERT INTO demo (ID,input1) VALUES (2,'$value');

if (!mysql_query($sql)) {
	die('Error: ' . mysql_error());
}


if (!mysql_query($sql)) {
	die('Error: ' . mysql_error());
}

echo 'Connected successfully';

mysql_close();
?>

demo-form.php below - the form shows OK and when I submit, it goes to demo.php. I can see in the URL but returns a completely blank page, no error no nothing. And when I query the table only the one row I inserted in HeidiSQL shows not the one I am trying to add from my form.

Code:
<form action="demo.php" method="post" />
<p>Input 1: <input typr="text" name="input1" /><p>
<input type="submit" value=Submit />
</form>

TIA


DougP
 
Looks like your PHP installation is not set to return errors, because you have oh so many of them.

Missing all the semi colons for the the constant definitions at the beginning of the code.

A stray echo that has nothing to echo, and would also be missing the semi colon.

Missing semi colon at the end of the DB connection.

Missing a parenthesis after the first if statement.

These would all cause errors which would halt execution of the code, so you get a blank page.

You can use the following code at the top of your file to set error reporting to on,
Code:
<?php
  [blue]error_reporting[/blue]([red]E_ALL[/red]);
  [blue]ini_set[/blue]([red]"display_errors"[/red], [red]1[/red]);
?>

Note that it only works if there are no fatal errors that prevent execution, because then the error reporting lines don't get executed.

To get around that, its better to have the error reporting set in the php.ini configuration file if you have access to it.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
OK I got this to work
but how do I print whats in the results to the screen?
all I see is "Connected successfully"

Code:
<?php

$link = mysql_connect('myserver', 'user', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db('mydatabase', $link);

//SOLUTION::  add this comment before your 1st query -- force multiLanuage support
$result = mysql_query("set names 'utf8'");

$query = "Select * from Demo;";
$result = mysql_query($query); // <<< how do I print this to a grid or table or something.

echo 'Connected successfully';

mysql_close($link);

?>

TIA


DougP
 
Hi

DougP said:
how do I print this to a grid or table or something.
You will have to call one of these functions in a loop :
[ul]
[li][tt]mysql_fetch_array()[/tt][/li]
[li][tt]mysql_fetch_assoc()[/tt][/li]
[li][tt]mysql_fetch_field()[/tt][/li]
[li][tt]mysql_fetch_object()[/tt][/li]
[/ul]
I prefer the second one :
Code:
[navy]$result[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]mysql_query[/color][teal]([/teal][navy]$query[/navy][teal]);[/teal]
[b]while[/b] [teal]([/teal][navy]$row[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]mysql_fetch_assoc[/color][teal]([/teal][navy]$result[/navy][teal]))[/teal] [teal]{[/teal]
  [COLOR=darkgoldenrod]printf[/color][teal]([/teal]
    [green][i]'<tr><td>%s</td><td>%s</td></tr>\n'[/i][/green][teal],[/teal]
    [COLOR=darkgoldenrod]htmlspecialchars[/color][teal]([/teal][navy]$row[/navy][teal][[/teal][green][i]'field1name'[/i][/green][teal]]),[/teal]
    [COLOR=darkgoldenrod]htmlspecialchars[/color][teal]([/teal][navy]$row[/navy][teal][[/teal][green][i]'field2name'[/i][/green][teal]])[/teal]
  [teal]);[/teal]
[teal]}[/teal]

Feherke.
 
missing something
I get this
1test\n2test2\nConnected successfully

Code:
<?php

$link = mysql_connect('myweb', 'user', 'Pass');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db('mydb', $link);

//SOLUTION::  add this comment before your 1st query -- force multiLanuage support
$result = mysql_query("set names 'utf8'");

$query = "Select * from Demo;";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
  printf(
    '<tr><td>%s</td><td>%s</td></tr>\n',
    htmlspecialchars($row['ID']),
    htmlspecialchars($row['input1'])
  );
}
echo 'Connected successfully';

mysql_close($link);
?>

DougP
 
I found this on-line

Code:
<html>
<body>
<?php
$username="me";
$password="Pass";
$database="db";

mysql_connect('myweb',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM demo";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">ID</font></th>
<th><font face="Arial, Helvetica, sans-serif">input1</font></th>

</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"ID");
$f2=mysql_result($result,$i,"input1");
//$f3=mysql_result($result,$i,"field3");
//$f4=mysql_result($result,$i,"field4");
//$f5=mysql_result($result,$i,"field5");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>

</tr>

<?php
$i++;
}
?>
</body>
</html>

DougP
 
Hi

DougP said:
missing something
Yes, [tt]table[/tt] tags around. I tried to keep the code simple.
DougP said:
I get this
1test\n2test2\nConnected successfully
Oops. [tt]printf()[/tt]'s 1[sup]st[/sup] parameter should be enclosed in double quotes ( " ) to let the [tt]\n[/tt] be transformed into newline.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top