Inserting data MySQL - php

I am a beginner in MySQL and I'm trying to enter some values in my database. The database is already made and its based localy.
I have a Schema named 'test' and within the schema I have a table named 'contacts'. Within the contacts I have an id, first and last name.
So I am trying to enter some data in the contacts. This is my php file:
<?
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '*******'; // root pass
$db_database = 'contacts';
$link = #mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_database,$link);
$query = "INSERT INTO contacts VALUES ('','John','Smith')";
mysql_query($query);
mysql_close();
?>
When I run the file on my browser, my database doesnt get any values. How can I execute that file so that John Smith gets added to my database?

Your php file has to start with <?php instead of <?.
Try this:
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '*******'; // root pass
$db_database = 'contacts';
$link = #mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_database,$link);
$query = "INSERT INTO contacts VALUES ('','John','Smith')";
mysql_query($query);
mysql_close();
?>

Change
mysql_query($query);
to
mysql_query($query) or trigger_error(mysql_error() . $query);
then all will be revealed.

You might consider trying PHP ADODB and using AutoExecute with INSERT, if you know your field names
$db->AutoExecute('tablename',array('field1'=>'value','field2'=>'value'));
Can also be used for update:)
http://adodb.sourceforge.net/

First how is the structure of your table? If you have only 2 fields on the table that is the error if not please try to post the error the console gives back to you.
$query = "INSERT INTO contacts (first,last) VALUES ('John', 'Smith')";
$e=mysql_fetch_assoc($query);
also do not forget that your table structure says not null for the field id and the default value is null, so it will not accept this kind of insertion

Your ID is probably auto_increment, if so add order of attributes to your query :
INSERT INTO contacts(`first_name`, `last_name`) VALUES ('John','Smith')
Replace first_name and last_name with your real column names.

Related

How to create backup of one database into another database using php

I want to create backup of my website database in which is in MySQL in another database on regular basis , is it possible to do it using php
Already tried exporting database using php but requirement is something else
I think mysqldump is what you're looking for.
Export database A to SQL file to import to database B:
mysqldump --host=localhost --user=dbauser --password=dbapassword dba_name > /path/to/store/dba.sql
Import database A dump into database B:
cp /path/to/store/dba.sql | mysql --host-localhost --user=dbbuser --password=dbbpassword dbb_name
You can wrap these commands in a call to system() in a PHP script.
$host_name = "localhost";
$user_name= "root";
$password= "";
$database1 = "database_name";
$database2 = "second_database_name";
$con1 = mysqli_connect($host_name ,$user_name,$password,$database1);
$con2 = mysqli_connect($host_name ,$user_name,$password,$database2);
mysqli_select_db($con1,$database1)
mysqli_select_db($con2,$database2)
$sql = "SELECT id, firstname, lastname FROM users";
$result = mysqli_query($con1, $sql);
$result1 = mysqli_query($con2, $sql);

How can insert value to specific row in database

i have PHP file that post data to database sequentially ( row by row ) using url , i want to know how can post data at specific row and if there is data in that row replace it by new value
<?php
$dbusername = "root";
$dbpassword = "root";
$server = "192.168.137.150";
$dbconnect = mysql_pconnect($server, $dbusername, $dbpassword);
$dbselect = mysql_select_db("test",$dbconnect);
$sql = "INSERT INTO test.a0 (a0) VALUES ('".$_GET["value"]."')";
mysql_query($sql)
?>
Depending on logic you need in the table, usually you have to look for the entry with SELECT first and then UPDATE if exists or INSERT if missing (2 queries required: SELECT + UPDATE/INSERT).
You can also make that field key and directly use INSERT ... ON DUPLICATE KEY UPDATE in a single query.

PHP selects in Firebird database always return empty

I'm trying to use Firebird in my PHP application. I already managed to make interbase and Firebird PDO extensions work, and the connection is established without any problems.
But when I try to make a simple select into one of the tables (select * from filial), it always returns empty, just like there's nothing recorded in the table.
I already tested my script in another database, and it worked properly, so I guess it's not a PHP problem, but I think it has something with my database.
This is how I created the database and table with ISQL:
create database 'C:\My\Database\Path.fdb' page_size 4096 user 'myuser' password 'mypass' default character set UTF8;
connect 'C:\My\Database\Path.fdb' user 'myuser' password 'mypass';
create table filial (id int not null primary key, nome varchar(45));
insert into filial (id, nome) values (1, 'test');
When I run the 'select' query in ISQL, it returns the one inserted row. But doing it with interbase or PDO, I get an empty object.
I also tried using capital letters for the table and columns names.
What am I doing wrong?
I'm running this project in a Windows 7, with WAMP and Firebird server installed.
Interbase PHP code:
$db_server = 'localhost';
$db_user = 'SYSDBA';
$db_passw = 'masterkey';
$db_name = 'C:\Users\Joao\Firebirds\Desenvolvimento.fdb';
$host = $db_server.':'.$db_name;
$dbh = ibase_connect($host, $db_user, $db_passw);
$stmt = 'select * from filial';
$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
echo $row->ID.'<br />';
}
ibase_free_result($sth);
ibase_close($dbh);
PDO PHP code:
$db_server = 'localhost';
$db_user = 'SYSDBA';
$db_passw = 'masterkey';
$db_name = 'C:\Users\Joao\Firebirds\Desenvolvimento.fdb';
$str_conn='firebird:host='.$db_server.';dbname='.$db_name;
$conn = new PDO($str_conn, $db_user, $db_passw);
$q = $conn->prepare('SELECT * FROM filial;');
$q->execute();
$dados = $q->fetchAll(PDO::FETCH_OBJ);
foreach($dados as $row){
echo $row->ID.'<br/>';
}
As I'm working locally, I also put connection data.

INSERT table row to another table

I have two identical tables in my database. I'm trying to ask the user the package number, then when the user clicks a button, it will copy the row matching the user input to another table.
My tables are:
awb - where the original data is.
temp - table to insert the data into.
Here's my code:
$dbhost = "localhost";
$dbuser = "root";
$dbname = "outbound";
mysql_connect($dbhost, $dbuser);
mysql_select_db($dbname) or die(mysql_error());
$packNO = $_GET['packNO'];
// Escape User Input to help prevent SQL Injection
$packNO = mysql_real_escape_string($packNO);
//build query
$query_add="INSERT INTO temp FROM awb WHERE packNO = '$packNO'";
#mysql_query($query_add);
$query = "SELECT * FROM temp";
$qry_result = mysql_query($query) or die(mysql_error());
The code that follows outputs the content of the temp table. But when I print it, I get nothing.
Why is the temp table empty when I print its values?
Try something like this
$query_add="INSERT INTO temp SELECT * FROM awb WHERE packNO = '$packNO'";
Documentation
Similar query, but not using *
$query_add="INSERT INTO temp (packNO, name)
SELECT packNO, name
FROM `awb`
WHERE `packNO` = '$packNO'";

prevent duplicate entries mysql

Hello guys I have a problem with a script
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'site';
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$con)
{
die(mysql_error());
}
mysql_select_db($dbname, $con);
$sql = "REPLACE INTO stiri (id, titlu, continut, link, categorie, data) VALUES ('','$titlu','$text','$link','Liga 1','$data')";
mysql_query($sql);
mysql_close($con);
This part is in a php foreach part and every time I run the script I get duplicate entry, how can I prevent this?
I could use UNIQUE Constraint but I want the link to be unique which is longer than 125 chars..
I'm guessing id is a primary key field in your table? You're trying to insert an empty string ('') into it. If that's an INT field, mysql will convert '' into 0. After the first such insert, you'll run into the duplicate key problem.
Change id to be an auto_increment field, and insert a null value, e.g.
REPLACE INTO stiri (id, ...) VALUES (null, ....)
so mysql can generate an ID for you automatically.

Categories