Codoforum related discussions
Good codes but datas doesnt insert into DB

Hi,
As a new PHP programer I am testing to insert datas into my database. Every things are showing me that the codes are well. For example I test the connection to my database it's fine:
I am using PHP 7.3.10 on Windows 10
testconnection.php
$conn = new mysqli("localhost", "root", "", "login"smile;

if ($conn->connect_error) {

die("ERROR: Unable to connect: " . $conn->connect_error);

}

echo 'Connected to the database.
';

$result = $conn->query("SELECT * FROM usuario"smile;

echo "Number of rows: $result->num_rows";

$result->close();

$conn->close();

?>

//I receive this message:

Connected to the database.
Number of rows: 2
Inserttodb.php

$link=mysqli_connect("localhost","root",""smile;
mysqli_select_db($link,"phpmysqli"smile;
?>

if(isset($_POST["submit"]))
{

mysqli_query($link,"insert into table1 values('$_POST[t1]','$_POST[t2]'smile"smile;

}

?>

When I insert data ist doesnt show any error. But when I check in phpMysql there is nothing. Ouuuf, I passed days to resolv this. But nothing!!!

Hi, As a new PHP programer I am testing to insert datas into my database. Every things are showing me that the codes are well. For example I test the connection to my database it's fine: I am using PHP 7.3.10 on Windows 10 testconnection.php $conn = new mysqli("localhost", "root", "", "login"); if ($conn->connect_error) { die("ERROR: Unable to connect: " . $conn->connect_error); } echo 'Connected to the database. '; $result = $conn->query("SELECT * FROM usuario"); echo "Number of rows: $result->num_rows"; $result->close(); $conn->close(); ?> //I receive this message: Connected to the database. Number of rows: 2 Inserttodb.php $link=mysqli_connect("localhost","root",""); mysqli_select_db($link,"phpmysqli"); ?> if(isset($_POST["submit"])) { mysqli_query($link,"insert into table1 values('$_POST[t1]','$_POST[t2]')"); } ?> When I insert data ist doesnt show any error. But when I check in phpMysql there is nothing. Ouuuf, I passed days to resolv this. But nothing!!!

Try the following please:

<?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'host';
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysql_select_db($dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysql_query($test_query);
$tblCnt = 0;
while($tbl = mysql_fetch_array($result)) {
  $tblCnt++;
  #echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "There are $tblCnt tables<br />\n";
}

Name the file db-connect-test.php.

Also think about if its mysqli or PDO.

If its mysqli then try the following script please:

<?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'host';
$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($link, $test_query);
$tblCnt = 0;
while($tbl = mysqli_fetch_array($result)) {
  $tblCnt++;
  #echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "There are $tblCnt tables<br />\n";
}

Let me know Your test result please.

Kind regards, Andy

Try the following please: ```` &lt;?php # Fill our vars and run on cli # $ php -f db-connect-test.php $dbname = &#039;name&#039;; $dbuser = &#039;user&#039;; $dbpass = &#039;pass&#039;; $dbhost = &#039;host&#039;; $connect = mysql_connect($dbhost, $dbuser, $dbpass) or die(&quot;Unable to Connect to &#039;$dbhost&#039;&quot;); mysql_select_db($dbname) or die(&quot;Could not open the db &#039;$dbname&#039;&quot;); $test_query = &quot;SHOW TABLES FROM $dbname&quot;; $result = mysql_query($test_query); $tblCnt = 0; while($tbl = mysql_fetch_array($result)) { $tblCnt++; #echo $tbl[0].&quot;&lt;br /&gt;\n&quot;; } if (!$tblCnt) { echo &quot;There are no tables&lt;br /&gt;\n&quot;; } else { echo &quot;There are $tblCnt tables&lt;br /&gt;\n&quot;; } ```` Name the file **db-connect-test.php**. Also think about if its mysqli or PDO. If its mysqli then try the following script please: ```` &lt;?php # Fill our vars and run on cli # $ php -f db-connect-test.php $dbname = &#039;name&#039;; $dbuser = &#039;user&#039;; $dbpass = &#039;pass&#039;; $dbhost = &#039;host&#039;; $link = mysqli_connect($dbhost, $dbuser, $dbpass) or die(&quot;Unable to Connect to &#039;$dbhost&#039;&quot;); mysqli_select_db($link, $dbname) or die(&quot;Could not open the db &#039;$dbname&#039;&quot;); $test_query = &quot;SHOW TABLES FROM $dbname&quot;; $result = mysqli_query($link, $test_query); $tblCnt = 0; while($tbl = mysqli_fetch_array($result)) { $tblCnt++; #echo $tbl[0].&quot;&lt;br /&gt;\n&quot;; } if (!$tblCnt) { echo &quot;There are no tables&lt;br /&gt;\n&quot;; } else { echo &quot;There are $tblCnt tables&lt;br /&gt;\n&quot;; } ```` Let me know Your test result please. Kind regards, Andy

45 years old and still not smarter lol

Check my GitHub Repos: https://github.com/WebCrew

My CrazyMates Account: https://crazymates.de/Crazymate

Cute but Evil Community: https://cute-but-evil.de

edited Dec 14 '19 at 11:49 am
14
1
2
live preview
enter atleast 10 characters
WARNING: You mentioned %MENTIONS%, but they cannot see this message and will not be notified
Saving...
Saved
With selected deselect posts show selected posts
All posts under this topic will be deleted ?
Pending draft ... Click to resume editing
Discard draft