Jump to content

[SOLVED] simple insert in table


Recommended Posts

hi,

please kindly check my query

it has an error when executed

<?php 
$sql="INSERT INTO tbl_exceed (address, city, phone)
VALUES
('$_POST['$data[2]']',
'$_POST['$data[3]']','$_POST['$data[4]']')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con)
?>

 

thanks

 

Link to comment
https://forums.phpfreaks.com/topic/104297-solved-simple-insert-in-table/
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

// Connecting, selecting database
$con = mysql_connect('asecasiasdi', 'root', 'itsysdev')
   or die('Could not connect: ' . mysql_error());
echo '<b>Step 1:</b> Connected successfully! <BR>';

$db = 'aai_dbexceed';
mysql_select_db($db) or die('Could not select database ('.$db.') because of : '.mysql_error());
echo '<b>Step 2:</b> Connected to ('.$db.') successful!<BR>';


?>

<?php 
$sql="INSERT INTO tbl_exceed (address, city, phone)
VALUES
('$_post[\"$data[2]\"]',
'$_post[\"$data[3]\"]',
'$_post[\"$data[4]\"]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con)
?>
</body>
</html>

<?php

// Connecting, selecting database
$con = mysql_connect('asecasiasdi', 'root', 'itsysdev')
   or die('Could not connect: ' . mysql_error());
echo '<b>Step 1:</b> Connected successfully! <BR>';

$db = 'aai_dbexceed';
mysql_select_db($db) or die('Could not select database ('.$db.') because of : '.mysql_error());
echo '<b>Step 2:</b> Connected to ('.$db.') successful!<BR>';


?>

<?php 
$sql="INSERT INTO tbl_exceed (address, city, phone)
VALUES
('$_post[\"$data[2]\"]',
'$_post[\"$data[3]\"]',
'$_post[\"$data[4]\"]')";
if (!mysql_query($sql,$con))
 {
 die('Error: ' . mysql_error());
 }
mysql_close($con);
?>

 

It was the last line in the code.

can anyone knows what is the error in my script?

<?php 
$sql="INSERT INTO tbl_exceed (address, city, phone)
VALUES
('$_post[\"$data[2]\"]',
'$_post[\"$data[3]\"]',
'$_post[\"$data[4]\"]')";
if (!mysql_query($sql,$con))

mysql_close($con);
?>

error message:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\exceed\upload.php on line 26

<?php 
$sql="INSERT INTO tbl_exceed (address, city, phone)
VALUES
('".$_post['$data[2]']."', '".$_post['$data[3]']".', '".$_post['$data[4]']".')";

if (!mysql_query($sql,$con))
{
mysql_close($con);
}
?>

 

you could try that too. i did not test at all so there might be small errors but i think it should work

another same errror...  :'(

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\xampp\htdocs\exceed\upload.php on line 26

line 26 is

('".$_post['$data[2]']."', '".$_post['$data[3]']".', '".$_post['$data[4]']".')";

:'(

it executed but no data were added.  ???

this is the source of the $data[], how to insert the $data[2], $data[3], $data[4]?

from this script to database above?

<?php
$text = $_REQUEST['fname'];
$file = file("$text");
$a = $file;
$state = $_REQUEST['state'];
$newfile = $_REQUEST['txtfile'];
$count = 0;
foreach ($a as $row => $line)

{
//$data[0] = trim(substr($line,  0, 16))."<br />";
//$data[1] = trim(substr($line, 16,125))."<br />";

$data[2] = trim(substr($line,141, 55))."<br />";

$data[3] = trim(substr($line,196, 32))."<br />";

$data[4] = trim(substr($line,228, 14))."<br />";

//$data[5] = trim(substr($line,240,  2))."<br />";
if ($data[2]!= "<br />" or $data[3] != "<br />" or $data[4] != "<br />") $final[] = $data;
$count++;
}

echo "There are " . $count . ": Records Found";
?>

<table width="100%" border="1">
<th bgcolor="#CCCCCC">ADDRESS</th><th bgcolor="#CCCCCC">CITY</th><th bgcolor="#CCCCCC">STATE</th><th bgcolor="#CCCCCC">PHONE</th><th bgcolor="#CCCCCC">ZIP CODE</th>
  <tr>
  <?php for($i = 0;$i <=$count; $i++)
{
$data = $final[$i];?>
    <td><?php echo $data[2];?></td>
    <td><?php echo $data[3];?></td>
    <td><?php echo $state;?></td>
    <td><?php echo $data[4];?></td>
</tr>

<?php } ?> 

please....

yes i have a form

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UDAC</title>
</head>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<table border="0">
<tr><td><b>Filename:</b></td><td><input name="fname" type="text" /></td>
<br />
<tr><td><b>State:</b> </td><td><input name="state" type="text" /></td>
<tr><td colspan="2"><input name="submit" type="submit" value="load"  /></td>
</table>
</form>

</form>
<form action="upload.php" method="GET" enctype="multipart/form-data">
<input name="submit" type="submit" value="save database" />
</form>
<?php
$text = $_REQUEST['fname'];
$file = file("$text");
$a = $file;
$state = $_REQUEST['state'];
$newfile = $_REQUEST['txtfile'];
$count = 0;
foreach ($a as $row => $line)

{
//$data[0] = trim(substr($line,  0, 16))."<br />";
//$data[1] = trim(substr($line, 16,125))."<br />";

$data[2] = trim(substr($line,141, 55))."<br />";

$data[3] = trim(substr($line,196, 32))."<br />";

$data[4] = trim(substr($line,228, 14))."<br />";

//$data[5] = trim(substr($line,240,  2))."<br />";
if ($data[2]!= "<br />" or $data[3] != "<br />" or $data[4] != "<br />") $final[] = $data;
$count++;
}

echo "There are " . $count . ": Records Found";
?>

<table width="100%" border="1">
<th bgcolor="#CCCCCC">ADDRESS</th><th bgcolor="#CCCCCC">CITY</th><th bgcolor="#CCCCCC">STATE</th><th bgcolor="#CCCCCC">PHONE</th><th bgcolor="#CCCCCC">ZIP CODE</th>
  <tr>
  <?php for($i = 0;$i <=$count; $i++)
{
$data = $final[$i];?>
    <td><?php echo $data[2];?></td>
    <td><?php echo $data[3];?></td>
    <td><?php echo $state;?></td>
    <td><?php echo $data[4];?></td>
</tr>

<?php } ?> 



</table>

</body>
</html>

what im trying to do is i need to insert the data from here to the database.

and the script that will insert the data to db is script above from previous post.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.